I'm pulling an actor from the database. In the actor's view I want to see a list of all movies my actor participated in. For each movie he starred in, I want to also show every role he had.
So basically the actor's overview page would look like this:
The Man With The Iron Pizza Writer, Actor
No Pizza For Old Men Actor
I'm getting my results in my model as follows:
public function movies() {
return $this->belongsToMany('Movie', 'movie_credits', 'person_id', 'movie_id')
->withPivot('role_id')
->join('role', 'movie_credits.role_id', '=', 'role.role_id');
}
Ideally I'd like to be able to output my results as follows, but being new to Laravel, I'm not sure how I can add a subobject 'roles' to each movie.
@foreach($person->movies as $movie)
<h3>{{ $movie->title }} </h3>
@foreach($movie->roles as $role)
<li>{{ $role->title }}</li>
@endforeach
@endforeach
If someone has a suggestion, I would love to hear it, because I'm going to need this way of grouping/sorting a lot more.
Thanks in advance!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community