Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lukeboy_2002's avatar

Sort by role

Hello,

Im made a livewire component to view all User with search and a sorting. To sort by role I used a left join in my query. But now in my column role only the role ID is rendered. But I want the role name.

My livewire component

<?php

namespace App\Livewire\Admin\Users;

use App\Models\Permission;
use App\Models\User;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;

class All extends Component
{
    use WithPagination;

    public $delete_id;

    #[Url(history:true)]
    public $search ='';

    #[Url(history:true)]
    public $sortBy = 'created_at';

    #[Url(history:true)]
    public $sortDir = 'DESC';

    #[Url()]
    public $perPage = 10;

    public function updatedSearch(){
        $this->resetPage();
    }

    public function delete(User $user){
        $user->delete();
    }

    public function setSortBy($sortByField){

        if($this->sortBy === $sortByField){
            $this->sortDir = ($this->sortDir == "ASC") ? 'DESC' : "ASC";
            return;
        }

        $this->sortBy = $sortByField;
        $this->sortDir = 'DESC';
    }

    public function render()
    {
        return view('livewire.admin.users.all', [
            'users' => User::search($this->search)
                ->leftJoin('model_has_roles as role', 'id', '=', 'role.model_id')
                ->orderBy($this->sortBy,$this->sortDir)
                ->withTrashed()
                ->paginate($this->perPage)
        ]);
    }
}

In my blade I solved like this:

@if( $user->role_id =='1' )
 	User
@endif
@if( $user->role_id =='2' )
	Admin						
@endif
@if( $user->role_id =='3' )
	Member
@endif

But when a new role is created it will not be rendered

How can I solve this. For Roles and Permmisions I use Spatie and for Users I use Jesteam

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To display the role name instead of the role ID in your Livewire component, you can modify your query to include the role name from the roles table. Here's how you can do it:

  1. Make sure you have a roles table in your database that stores the role names.

  2. Update your render method in the All component to include the role name in the query:

public function render()
{
    return view('livewire.admin.users.all', [
        'users' => User::search($this->search)
            ->leftJoin('model_has_roles as role', 'id', '=', 'role.model_id')
            ->leftJoin('roles', 'role.role_id', '=', 'roles.id') // Join the roles table
            ->select('users.*', 'roles.name as role_name') // Select the role name
            ->orderBy($this->sortBy, $this->sortDir)
            ->withTrashed()
            ->paginate($this->perPage)
    ]);
}
  1. In your blade file, you can now access the role name using $user->role_name:
@if($user->role_name == 'User')
    User
@endif
@if($user->role_name == 'Admin')
    Admin
@endif
@if($user->role_name == 'Member')
    Member
@endif

This way, whenever a new role is created, it will be rendered correctly based on its name.

Please or to participate in this conversation.