Support the ongoing development of Laravel.io →
Database Eloquent

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?

Last updated 3 years ago.
0

Any ideas?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jerauf jerauf Joined 16 Feb 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.