Support the ongoing development of →
Database Eloquent
Last updated 10 months ago.

The $relation->save($model) method automatically fills in the appropriate foreign id, so you shouldn't need to set it.

I believe the issue here is you're using a $user model that you got via dependency injection. That's going to be the general User facade, not an instantiated model. Hence it probably doesn't have an id, and when save($task) gets called, it overrides the existing $task->user_id with $user->id, which is NULL.

Luckily Auth::user() returns a User object, so you can just call this without fussing with any ids whatsoever:

Last updated 6 years ago.

Thank you very much ;) I'm gonna keep this in mind.


Sign in to participate in this thread!


Your banner here too?

SzkolaWEB szkolaweb Joined 26 Mar 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.