wizjo
1 year ago
151
11
Laravel

Debugbar shows thousands gate queries

Posted 1 year ago by wizjo

In my application, I have users, from whom everyone can have one role, and role has many permissions.

I store permissions in database and attach it to roles.

Recently I have installed great package Debugbar (https://github.com/barryvdh/laravel-debugbar) which shows me in some views (for example users listing page) an alarmingly large number (over 2000) in Gate tab.

This number is connected with using @can directive in Blade views.

I wonder if this is a sign that something is worth optimizing, because it slows down my application or is it normal and the use of such directives is the optimal solution.

I'm not sure if this will be needed to recognize the problem, but I also paste a few places from my role-related code:

Trait HasRoles used in User model

trait HasRoles
{
    public function role()
    {
        return $this->belongsTo(Role::class);
    }

    public function hasRole($roles)
    {
        if (count($roles) > 0) {
            foreach ($roles as $role) {
                if ($role->name == $this->role->name) {
                    return true;
                }
            }

            return false;
        }

        return ($this->role->name == $roles);
    }

    public function hasPermission(Permission $permission)
    {
        return $this->hasRole($permission->roles);
    }
}

Please sign in or create an account to participate in this conversation.