Level 75
In User model define your relationship correctly.
public function courses()
{
return $this->hasMany(Course::class, 'Teacher_id');
}
Docs: https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
in one of my tables "courses" i have a value "Teacher_id" which is a foreign key that references "id" on "users" table. when i try to do mass assignment for the request as follows: auth()->users()->courses()->create($request->course)
it throws an error because it cant find users_id, I solved this by assigning each value separately but im looking to make life easier and do it with "mass assignment" . Any suggestions?
In User model define your relationship correctly.
public function courses()
{
return $this->hasMany(Course::class, 'Teacher_id');
}
Docs: https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
Please or to participate in this conversation.