their relationship would be like this: movie has one/many movieDirection and movieDirection has one movie directorhas one/many movieDirection and movieDirection has one director
Jul 29, 2020
6
Level 1
eloquent relationship between 3 tables
I wanna get the director of a movie using this tables
movie -mov_id -mov_title
director -dir_id -dir_name
movieDirection -mov_id -dir_ID
I cant figure it out especially because Im new to eloquent relationship. Should I make 3 models for each tables?
Level 55
Calling "pivot" returns you data from intermediate table. By the way, you need to specify the data you want to load from intermediate table manually on relations (only keys are included in pivot by default).
class Movie extends Model
{
public function directors()
{
return $this->belongsToMany(Director::class)->withPivot('date');
}
}
class Director extends Model
{
public function movies()
{
return $this->belongsToMany(Movie::class)->withPivot('date');
}
}
echo $movie->directors->first()->pivot->date;
1 like
Please or to participate in this conversation.