Support the ongoing development of Laravel.io →
Database Eloquent Installation
Last updated 2 years ago.
0

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)
0

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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2024 Laravel.io - All rights reserved.