phplaravellaravel-5eloquentmass-assignment

Mass Assignment issue with `updateOrCreate` in Laravel, why?


I have a very simple model:

class Employee extends \Eloquent
{
}

And what I would like to do is to synchronize the local data with the ones stored in a remote server. In my MySQL I have

PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)

So I am sure that if I match on id, I would never do mass assignment.

So when I do:

foreach((new RemoteEmployee())->all() as $emp) {
    Employee::updateOrCreate(['id' => $emp['id']], [
        'firstname' => $emp['id'], 
        'lastname' => $emp['lastname'], 
        'phone' => $emp['phone']
    ]);
}

I should not get this error:

Add [id] to fillable property to allow mass assignment on [App\Models\Employee].

Anyway, if I modify my model as follow:

class Employee extends \Eloquent
{
    protected $fillable = ['*'];
}

I do not get any errors, but my entries are null.

Why?


Solution

  • First exclude the id from the fields

    Second if you want to allow all fields use protected $guarded = []; instead.