show the complete statement?
I assume you have imported Role?
Are you calling this in the render method, the mount method or elewhere?
Show more of the error message, like what line it occurs on
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi all. I need your help.
I am using laravel_permission (https://spatie.be/docs/laravel-permission/v4) and Livewire.
In tinker I make a request:
Role::with('permissions')->withCount('users')->get()
Everything goes fine as I expect it to.
But when I transfer the same line of code to the Livewire component, I get an error:
Class name must be a valid object or a string
I can't figure out what the problem is.
Very strange.
If I use this in the Roles component:
<?php
namespace App\Http\Livewire\Backend;
use Livewire\Component;
use Spatie\Permission\Models\Role;
class Roles extends Component
{
public function render()
{
// $roles = Role::with('permissions')->withCount('users')->get();
$roles = Role::with('permissions')->get();
return view('livewire.backend.roles', compact('roles'));
}
}
and this in the roles.blade.php:
@foreach($roles as $role)
<tr class="text-sm">
<td class="py-1">{{ $role->label ?: '-' }}</td>
<td class="py-1 text-center">{{ $role->name }}</td>
<td class="py-1 text-center">{{ $role->permissions->pluck('name')->implode(', ') ?: 'Без ограничений' }}</td>
<td class="whitespace-nowrap py-1 text-center">{{ $role->users->count() }}</td>
</tr>
@endforeach
all works fine.
Please or to participate in this conversation.