I'm trying to eager load a list of credits for a recording. The query I want to run is this:
SELECT * FROM credits WHERE recording_id = $id AND credit_class = 'CA' ORDER by sort_no LIMIT 5
When I do this in my DB, it returns exactly what I want.
But when I do it like this, it returns 1.
Recording::with(array('recording_credits' => function($studio_credits_query)
{
$studio_credits_query->with('credit_name')
->where('credit_class', '=', 'CA')
->orderby('sort_no')
->take(5)
->get();
}))->where('alt_id', '=', $id)
->where('rec_class', '=', 'S')
->orderBy('year_rec')
->get();
The model for Recording is:
class Recording extends Eloquent
{
public function recording_credits()
{
return $this->hasMany('Credit', 'recording_id', 'id');
}
}
So, I want to return all recordings whose alt_id matches the page id (this is required for reasons that are too deep to go into here) and then under that I want to return 5 of the credits for each recording. Some recordings have 1 credits, others have 20. I need it to find the credits, sort them and take the first 5.
What am I doing wrong?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community