Skip to content
Advertisement

LARAVEL how to change $fillable in Model from trait?

I have in model:

use seoTrait;

protected $fillable = [
    'name', 'title', 'description'
];

I created trait “seoTrait” which need “seoMeta” in $fillable.

Now I add :

protected $fillable = [
    'name', 'title', 'description', 'seoMeta'
];

But is it possible in trait “seoTrait” add something to $fillable ?

Advertisement

Answer

Since Laravel 5.7, you can initialize a model trait with a magic initializeTraitName method on the trait which allow access to the model instance.

trait SeoTrait
{
    public function initializeSeoTrait()
    {
        $this->fillable[] = 'seoMeta';
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement