gpt4 book ai didi

php - TCG\Voyager 没有得到关于关系的翻译

转载 作者:行者123 更新时间:2023-12-05 05:53:20 25 4
gpt4 key购买 nike

我为 Laravel (v8.0) 使用 Voyager 管理界面 (v.1.4)。Voyager 支持多种语言(官方文档:https://voyager-docs.devdojo.com/v/1.4-1/core-concepts/multilanguage)。

我有这样的关系:

  • 进程 belongsToMany WorkMachine
  • 过程有很多产品

过程模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;

class Process extends Model
{
use Translatable;

protected $translatable = ['name', 'meta_description', 'description'];

public function get_workmachine() {
return $this->belongsToMany(WorkMachine::class, 'process_workmachine');
}

public function get_products() {
return $this->hasMany(Product::class, 'process_product');
}

WorkMachine 型号:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;

class WorkMachine extends Model
{
use Translatable;

protected $translatable = ['name', 'meta_description', 'description'];

产品型号:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;

class Product extends Model
{
use Translatable;

protected $translatable = ['name'];

在我的 Controller 中:

$process = App\Models\Process::where('slug', $processSlug)
->with('get_workmachine')
->with('get_products')
->firstOrFail()->translate(app()->getLocale());

问题是:Process 可翻译文本有效,输出语言取决于 app()->getLocale()。但工作机器文本和产品文本未翻译。

我也尝试使用:

->with(['get_workmachine' => function ($query) { $query->withTranslation('de'); }])

但它没有被翻译。

转换关系的任何解决方案?

最佳答案

我找到了可能的解决方案!在我的 .blade 文件中,而不是:

@foreach(json_decode($process->get_workmachine) as $workmachine)
...
...
@endforeach

我添加了->translate(app()->getLocale()):

@foreach(json_decode($process->get_workmachine->translate(app()->getLocale())) as $workmachine)
...
...
@endforeach

这行得通!

关于php - TCG\Voyager 没有得到关于关系的翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69911193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com