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

kahen's avatar
Level 1

Laravel scope query only for users - exclude result for admin

I am very new into programming and trying to make some work on Laravel / Voyager admin panel.

I have a Customers table and on this table there is also Customer_Representative field where I can select / assign customer representatives from dropdown list.

What I am trying to achieve is I want each users with their assigned roles (Customer Representative) to list customers only assigned for them.

I am using this below code, but I am not able to see list of customers that other users created. I can only see the customers I / Admin created but If I login with their details I can see their assigned customers.

So as an admin I want to be able to see all customers, so I can also edit them with my login credentials.

current code I am using is this

public function scopeCurrentUser($query)

{
   return $query->where('Customer_Representative', Auth::user()->id);
}

there has been a suggestions as below too ;

public function scopeCurrentUser($query) { return $query->when(auth()->user()->is_not_admin, function ($q) { $q->where('Customer_Representative', auth()->user()->id) }); }

but it resulted as syntax error, unexpected token "}"

Waiting your feedbacks.

0 likes
16 replies
tykus's avatar

You're missing a semi-colon in the Closure:

public function scopeCurrentUser($query)
{
    return $query->when(auth()->user()->is_not_admin, function ($q) {
        $q->where('Customer_Representative', auth()->id());
    });
}

Personally, I wouldn't put HTTP level logic such as auth()->user() in the model like this

kahen's avatar
Level 1

@tykus Thank you Sir that's why I was getting that mistake. However this code didn't solve my issue :). All users still can see all customers.

Do you have any other suggestions ? Customers are still not filtered based on Customer Representative credentials on their login. They can see all of the customer list but I want them to see customers assigned to them only excluding Admin.

kahen's avatar
Level 1

@tykus

this is the code I have under customer app

namespace App;

use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Auth;

class Customer extends Model {

public function scopeUserFilter ($query)
{
    return $query->when(auth::user()->is_not_admin, function ($q) {
        $q->where('Customer_Representative', auth::user()->id());
    });
}

 

}

tykus's avatar

@kahen I asked about the query you are making for the full customer list (I expect it is in a Controller???)

kahen's avatar
Level 1

@tykus also I uploaded 2 photos.

so we have some customer representatives

eniskahya. com/img/ss2.png

we have some customers assigned to customer representatives.

eniskahya. com/img/ss1.png

what I want is I want each customer representative to see their own customers assigned to them. So customer rep 1 shouldnt see customers of customer rep 2.

But admin must be out of this rule and he must see all customers.

tykus's avatar

@kahen so share the relevant Eloquent query; not screenshots 🤷‍♂️

kahen's avatar
Level 1

@tykus I am using laravel / voyager - so its using default PageController - Its too long do you want me to copy / paste it here ?. I tried using scope for my Customer app. The first code of mine was like this;

public function scopeCurrentUser($query)
{
    return $query->where('Customer_Representative', Auth::user()->id);
}

and it was filtering customers based on customer representatives however as an admin I was not able to see customers because they were assigned to other customer representatives.

then I tried below code too after your correction;

public function scopeCurrentUser($query) { return $query->when(auth()->user()->is_not_admin, function ($q) { $q->where('Customer_Representative', auth()->id()); }); }

but this didnt do anything - everyone was able to see all customers with this code.

tykus's avatar

@kahen is this valid; where does this property come from?

auth()->user()->is_not_admin
kahen's avatar
Level 1

@tykus it might not be valid. Someone in different forums replied me as below;

You change the auth()->user()->is_not_admin to your condition for non-admin validation. The scope utilizes the ->when() function to append the condition to query only if the condition (in this case if authenticated user is not an admin) is true.

So if authenticated user is not an admin, his query will be filtered by Customer_Representative.

I didnt imagine this was going to be this tough, only filtering customers based on assigned users / representatives :) maybe I couldnt explain my problem properly.

tykus's avatar

@kahen if there is no property (or accessor) named is_not_admin; then auth()->user()->is_not_admin will be null (which is falsey). This means the constraint is never applied.

So, how do you know that a user is an admin?

kahen's avatar
Level 1

@tykus Yes you are right but because I don't have too much information about programming I thought is_not_admin a default property / code for laravel. Thats why I couldnt understand why code was not working properly.

I know if user is an admin or customer representative with their Roles.

When I check Roles table in my database I can see that 1 is assigned for Admin, 3 is assigned for Customer Representative

tykus's avatar
tykus
Best Answer
Level 104

@kahen is there a hasRole Method on User to check for the User’s role?

I don’t know your code so I can only guess….

EDIT it appears that Voyager has a hasRole method mixed in to using a Trait; so the scope should be:

public function scopeCurrentUser($query)
{
    return $query->when(auth()->user()->hasRole('admin'), function ($q) {
        $q->where('Customer_Representative', auth()->id());
    });
}

Again; I would suggest that the authenticated user should not be used in the context of the Model, but this should work

kahen's avatar
Level 1

@tykus I sorted it out tykus. Thank you for your guidance and patience.

so I am using this now

public function scopeCurrentUser($query) { return $query->when(auth()->user()->role_id === 3, function ($q) { $q->where('Customer_Representative', auth()->id()); }); }

role_id 3 is customer representative. So Role id 1 admin can see all customers and role_id 3 customer repsentatives can see their own customers only :)

tykus's avatar

@kahen IMHO that hasRole method will be more readable like I showed above. If you're all set; please mark the thread closed.

Please or to participate in this conversation.