0
I’m making a listerner to save when the user logs in, and prints the title error in User 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')
]);
}
added in this way, protected $fillable = [ 'name', 'email', 'password', 'user_id', 'datetime' ]; @fernandosavio o model->save() replaces create()?
– Fernando Santana
In the model
Access::class
?– fernandosavio
create is used for mass assignment, such as adding an array of arrays with multiple
Access
to be created. save is used to save a single model. So create requires the model to know which fields are allowed to be entered in batch. That’s why save doesn’t check the field$fillable
– fernandosavio
changed to Return $this->hasMany('App Access'); remains the same.
– Fernando Santana
tested with save too, error continues.
– Fernando Santana
There is a detail that is bothering me. Exception says that the error is because of a field
users_id
but the question isuser_id
.&#Czech if you don’t have one typo in your class.– fernandosavio
I got this fillable belongs to the Access model, not the users. Thanks @fernandosavio now I have another error to deal with. it’s rule he put the updated_at
,
created_at` no Insert?– Fernando Santana
If you change the value of the property
$timestamps
in your modelAccess
he doesn’t do that. Look here at Docs– fernandosavio