【发布时间】:2021-12-22 22:18:12
【问题描述】:
我将 Voyager 管理界面 (v.1.4) 用于 Laravel (v8.0)。 Voyager 支持多种语言(官方文档:https://voyager-docs.devdojo.com/v/1.4-1/core-concepts/multilanguage)。
我有这种关系:
- 进程属于多台工作机
- 流程有很多产品
流程模型:
<?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'];
在我的控制器中:
$process = App\Models\Process::where('slug', $processSlug)
->with('get_workmachine')
->with('get_products')
->firstOrFail()->translate(app()->getLocale());
问题是:处理可翻译文本有效,输出语言取决于app()->getLocale()。但是工作机文本和产品文本没有翻译。
我也尝试使用:
->with(['get_workmachine' => function ($query) { $query->withTranslation('de'); }])
但它没有被翻译。
有什么办法可以翻译这种关系吗?
【问题讨论】:
标签: php laravel relationship translate voyager