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:
auth()->user()->addTask($task);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community