Skip to content
Advertisement

query inside with eloquent

image address has 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 the users and on relation whose query did not match will be null .I want to pull only those users which do not null value on relation and order By accordingly.

Advertisement

Answer

You can use whereHas condition

Users::whereHas('address', function($query){
$query->where('location', 'NAXAL');
$query->order By('total');
})->get();
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement