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.
You can use whereHas condition
Users::whereHas('address', function($query){ $query->where('location', 'NAXAL'); $query->order By('total'); })->get();