Marking properties as fillable when using Laravel relationship functions

448 Views Asked by At

I have a Team model which can have many Site models attached, and has a createSite function to add sites:

public function createSite($domain) {
    $site = new Site(['domain' => $domain]);
    return $this->sites()->save($site);
}

Running this fails, as domain is not fillable. While easily fixed, I've read the docs on Mass Assignment and am trying to minimise which fields are fillable.

What's best practice here? Should I use $fillable or explicitly assign these properties (ie $site->domain = $domain)?

1

There are 1 best solutions below

0
Usman Ali On

you can also protected $guarded = [] it is same as fillable but its functionality is opposite to the fillable you can read about it on the official web.