MassAssignmentException in Model.php line 444: users_id

0

I'm doing a listerner to save when the user logs, and prints the title error in User's models.

public function accesses()
    {
        // Não esqueça de usar a classe Access: use App\Models\Access;
        return $this->hasMany(Access::class);
    }

public function registerAccess()
    {
        // Cadastra na tabela accesses um novo registro com as informações do usuário logado + data e hora
        return $this->accesses()->create([
            'user_id'   => $this->id,
            'datetime'  => date('Y-m-d H:i:s')
        ]);
    }
    
asked by anonymous 04.09.2018 / 22:03

1 answer

1

Errors of MassAssignment usually occur because the inserted field is not in the $fillable property of the Model or is protected by the $guarded property of the Model .

In your case you should check that user_id and datetime are as fillable in the Access model. Or use the relationship->save() method to create the record.

Example using save() :

public function registerAccess()
{
    return $this->accesses()->save(new Access([
        'user_id'   => $this->id,
        'datetime'  => date('Y-m-d H:i:s')
    ]);
}
    
04.09.2018 / 22:06