Level 27
@jrdavidson You are ordering the roles of your users right now, but not users by their role. You would have to use a join in order to achieve what you want, which can get a bit tricky, since users can have multiple roles.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm working with Spatie's permissions package and trying to order my users by their role but applying a specific order of those roles by the role name. Currently with my query it its not ordering and I'm curious to know if I'm doing something wrong here.
public function scopeOrderByRole($query)
{
return $query->whereHas('roles', function ($query) {
return $query->orderBy(DB::raw('
case
when roles.name = "super-admin" then 1
when roles.name = "company-admin" then 2
when roles.name = "maintainer-admin" then 3
when roles.name = "user" then 4
end
'), 'desc');
});
}
Please or to participate in this conversation.