Back

Join interfering with eager loading


jerauf posted 3 years ago

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?

jerauf replied 3 years ago

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

giovannipds replied 4 months ago

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


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us