Support the ongoing development of Laravel.io →
Database Eloquent

I'm going insane.

I have a query that needs to use a join and eager loading. The join needs to occur because I need to use a field in the joined table in my where clause.

Here is my code:

						  $staging_query = CreditRole::join('roles', 'roles.id', '=', 'credits_roles.role_id')
									->with(array('has_credit' => function($staging_name_query)
						  				{
							  				$staging_name_query->with('has_name')
					  								->where('run_type', '=', 'OR')
					  								->orderBy('sort_no', 'asc')
					  								->get();
						  				}))
									->with('has_replacements')
									->where('roles.role_category', '=', 'ST')
									->orderBy('credits_roles.sort_no')
									->get(array('*', 'credits_roles.id as credit_id'));

Here is my model:

class CreditRole extends \Eloquent {

  protected $table = 'credits_roles';

  public function has_credit()
  {
	return $this->hasMany('\App\Models\CreditName', 'credit_role_id', 'id');
  }

}

The problem is that using this, it finds the roles correctly but doesn't display results from has_credit. If I eager load the role, it works as intended and displays has_credit. However, I need to query on roles.role_category.

Can anyone help?

Last updated 2 years ago.
0

Anyone have any ideas? Would be much appreciated. My project has stalled because of this.

0

Did you solved it? I've solved it using fromRaw & whereRaw, I couldn't get it working by using the normal workflow join + with...

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.