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

eugenefvdm's avatar

Scope Roles in Nova Resources with Spatie Permissions Library

I have a stock setup of Nova with User and Client models. Web guard has been bound to Client as per documentation.

Now I want Nova resources for users and clients.

Users have the "User" role, and Clients have the "Client" role.

I want to only see Clients when I load the Nova resource, so I my first instinct is a scope to display Clients but filter on hasRole('Client');

The scope syntax throws me off though, and doesn't work.

<php
class ClientScope implements Scope
{
    
    public function apply(Builder $builder, Model $model)
    {
        $builder->where($model->hasRole('Client'));
    }

}

The error returned:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from `users` where `` is null order by `users`.`id` desc limit 26 offset 0) {"userId":1,"exception":"[object] (Illuminate\Database\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found:

Any ideas how I can get a list of models filtered by role for beautiful Nova displaying?

I'm sure this should be trivial but my Eloquent is letting me down.

0 likes
2 replies
Kinomej's avatar

Hi there,

Could you provide the hasRole method?

The ->where() method requires the first parameter to be the column you want to filter.

I would suggest it could work like this:

$builder->where('Role', '=', 'Client')

or something like this:

$builder->where('Role', '=', $model->getRole())

or like this if you want to use your models Method:

$builder->whereRaw('true = ' .$model->hasRole('Client') )

I would not recomment this way tho. :)

eugenefvdm's avatar

Hi @kinomej , thanks so much for the reply.

I battled for a little bit and then just googled something like 'join in laravel builder model scope'. This is the solution for now, just an old school join. You'll notice that I have to work on model_has_roles as there is no Role in the User or Client tables.

<?php

class ClientScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $builder
            ->join('model_has_roles', 'model_has_roles.model_id', '=', 'users.id')
            ->where('model_has_roles.model_type', "App\Client");
    }
}
2 likes

Please or to participate in this conversation.