Use a left join instead :)
Feb 10, 2023
5
Level 1
orderBy a whereHas column without ignoring the unrelated data?
I have got users table related to a roles table "many to many relationship"; users are related to the roles and I want to order them by the role name so I built this query
User::whereHas('roles', function($query) {
return $query->orderBy('name');
});
it does what I want.. but if some users are not related to any roles they are not fetched.. I want to fetch them too but list them at the end of the records maybe.. How can I do that?
Level 1
Solved by this solution https://stackoverflow.com/a/72667602/20556973
Please or to participate in this conversation.