Home  >  Q&A  >  body text

Using Eloquent for internal querying

Image address contains location and total number of users.

Users::with(['address'=>function($query){
    $query->where('location', 'NAXAL');
    $query->order By('total');
    }])->get();

This query will return all users, and relationships not matched by the query will be empty. I only want to extract those users who are not null on the relationship and sort accordingly.

P粉056618053P粉056618053194 days ago600

reply all(1)I'll reply

  • P粉627136450

    P粉6271364502024-04-07 16:01:40

    You can use whereHas condition

    Users::whereHas('address', function($query){
    $query->where('location', 'NAXAL');
    $query->order By('total');
    })->get();

    reply
    0
  • Cancelreply