Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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);
Last updated 8 years ago.
0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

SzkolaWEB szkolaweb Joined 26 Mar 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.