How can I make localized a non-localized model in Larvel Backpack?

395 Views Asked by At

I have a non-localized model: Article with the following properties: id, title, description; and I want to localize title and description.

I have seen in the Laravel Backpack documentation that CRUDs can manage localized models using spatie/laravel-translatable, which uses JSON fields for each property Translatable models and multi-language CRUDs.

I think it's a good way to manage new localized models but I can't find a way to pass my Article model to a localized-version without rewriting a big part of the existing software.

There is a good practice I'm missing to do it?

1

There are 1 best solutions below

1
On

If you already using Backpack then using spatie/laravel-translatable is best option. You can make your Article model like below to make as translatable mode.

<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations; //you need to add

class Article extends Model
{
    use CrudTrait;
    use HasTranslations; //you need to add

     /*
  |--------------------------------------------------------------------------
  | GLOBAL VARIABLES
  |--------------------------------------------------------------------------
  */

    protected $table = 'articles';
    protected $primaryKey = 'id';
    protected $fillable = ['title', 'description'];
    protected $translatable = ['title', 'description']; //you need to add