Have you tried eager loading with constraint?
$attribute_id = '5e65e9d4-ee60-11e6-994d-40167eb29fbd';
$q = Profile::with(['ProfileAttributes' => function($q) use($attribute_id){
$q->where('attribute_id', $attribute_id)
}])->where('completed', true)
->with(['State', 'City', 'Country', 'Agency'])
->orderBy('created_at', 'desc')
->orderBy('state_id', 'asc')
->orderBy('name', 'asc')
->paginate(24)
I think you need to build your subquery. $q is new there...
So try this:
public function scopeSearchHairColor($query, $attribute_id) {
$query->with(['ProfileAttributes'])
->whereIn('attribute_id', function ($q) use ($attribute_id) {
$q->select('id')
->from('profile_attributes')
->where('attribute_id', '=', "$attribute_id");
});
}
Didn't test it, but since you're doing whereIn, the $query
that's being passed, is probably just a new instance of the query builder. It's - as you say - a subquery, hence has no relation to the outer query imo.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community