vendredi 5 juin 2020

Laravel - Extend Model $guarded from parent model

I have a BaseModel class which extends the Illuminate\Database\Eloquent\Model:

class BaseModel extends Model
{
    protected $guarded = ['id', 'company_id', 'identifier'];
}

In my other Model classes, I extend the BaseModel, and I am trying to extend $guarded from BaseModel in the child Models:

class Person extends BaseModel
{
    public function __construct(array $attributes = array())
    {
        parent::__construct($attributes);
        $this->guarded = array_merge($this->guarded, ['address', 'phone']); //This is that I'm trying to do
    }
}

However, it doesn't work. Is it possible to extend $guarded from parent Model or I need to declare all fields on child models too?

SOLUTION - Thanks to @Jignesh Joisar!

public function __construct(array $attributes = array())
{
    $this->guard(array_merge($this->getGuarded(), ['address', 'phone']));
    parent::__construct($attributes);
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3gQfovs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire