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

umairparacha's avatar

with or withCount with users relation shows an error laravel spatie permissions.

with or withCount works with permissions relation but when you add users relation it shows an error.

And also when you do it on tinker it goes completely fine. Very strange

My code

$roles = QueryBuilder::for(Role::class)
            ->allowedSorts('name')
            ->withCount('users')
            ->withCount('permissions')
            ->where('name', '!=', 'owner')
            ->where('guard_name', 'web')
            ->where('user_id', $user->id)
            ->get(['name', 'id']);
        return inertia('roles/staff/Index', ['roles' => $roles]);

Error

Class name must be a valid object or a string
0 likes
0 replies

Please or to participate in this conversation.