search

Home  >  Q&A  >  body text

Using Eloquent for internal querying

Image address contains location and total number of users.

1

2

3

4

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粉056618053383 days ago903

reply all(1)I'll reply

  • P粉627136450

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

    You can use whereHas condition

    1

    2

    3

    4

    Users::whereHas('address', function($query){

    $query->where('location', 'NAXAL');

    $query->order By('total');

    })->get();

    reply
    0
  • Cancelreply