António Almeida
António Almeida

Reputation: 10117

Laravel Backpack Fake fields and translations

I have a CRUD entity with a determined amount of entries. There are some common fields (title, description), but some entries have some exclusive fields, which are saved in extras.

My problem is, some fields may be translatable and others may not. Right now the only way to get translations to work in fake fields is to make all extras translatable.

class Module extends Model
{
    use CrudTrait;
    use HasTranslations;

    ...
    protected $fillable = ['title', 'description', 'extras'];
    public $translatable = ['title', 'extras'];

This causes me a problem, because many extra fields are images not translatable.

Upvotes: 1

Views: 2319

Answers (1)

António Almeida
António Almeida

Reputation: 10117

I found a solution with the help from @lloy0076

  1. Add a column to entity table, named extras_translatable, right after 'extras'.

  2. On entity Model, add extras_translatable to $fillable, $fakeColumns and $translatable variables, and cast 'extras_translatable' to array:

    protected $fillable = ['title', 'description', 'extras', 'extras_translatable'];
    protected $fakeColumns = ['extras', 'extras_translatable']; 
    protected $translatable = ['title', 'extras_translatable'];
    protected $casts = ['extras_translatable' => 'array'];
    
  3. Then just store the desired fields in extras_translatable:

    $this->crud->addField([
        'fake' => true,
        'store_in' => 'extras_translatable',
        ...
    ]);
    

Upvotes: 5

Related Questions