Trait for user Model

Hi there , I have a multi tenant project the tenants are restaurant name and users belong to one restaurant. I want to show all the users from the user resource when the logged in user has a Admin role. Otherwise, managers can only see restaurants belonging to the user , restaurant_id.

I was able to write a trait that works great when i have users logged in. Once I sign out the user I get the following message.

the code of the trait is:



<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait FilterByRestaurant
{
protected static function boot(): void
{

parent::boot();

$restaurantId = auth()->user()->restaurant_id;

self::creating(function($model) use ($restaurantId){
$model->restaurant_id=$restaurantId;
});

if ( ! auth()->user()->hasRole('Admin')) {
self::addGlobalScope(function(Builder $builder) use($restaurantId){
$builder->where('restaurant_id',$restaurantId);
});
}


}
}



Can you please help me to figure out what my issue is?

Thanks
image.png
image.png
Was this page helpful?