Support the ongoing development of Laravel.io →
Database Eloquent Blade

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:

Ben Galore

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!

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

plokkum plokkum Joined 18 Jun 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.

© 2025 Laravel.io - All rights reserved.