I use Voyager admin interface (v.1.4) for Laravel (v8.0). Voyager supports multiple languages (official documentation: https://voyager-docs.devdojo.com/v/1.4-1/core-concepts/multilanguage).
I have this relationship:
Process model:
<?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 model:
<?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'];
Product model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;
class Product extends Model
{
use Translatable;
protected $translatable = ['name'];
In my controller:
$process = App\Models\Process::where('slug', $processSlug)
->with('get_workmachine')
->with('get_products')
->firstOrFail()->translate(app()->getLocale());
The problem is: the Process translatable text works, the output language depends from app()->getLocale()
. But the workmachine texts and products texts aren't translated.
I try also to use:
->with(['get_workmachine' => function ($query) { $query->withTranslation('de'); }])
But it isn't translated.
Any solutions to translate the relationship?
I have found a possible solution! In my .blade file, instead of:
@foreach(json_decode($process->get_workmachine) as $workmachine)
...
...
@endforeach
I added ->translate(app()->getLocale())
:
@foreach(json_decode($process->get_workmachine->translate(app()->getLocale())) as $workmachine)
...
...
@endforeach
This works!