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

Kris01's avatar

Is this an excessive number of query calls?

Hi Guys, I am using AdminLte to build the admin panel. Using Laravel Debugber, I noticed that is doing like 50 query calls to load a simple view like 'show products', where I am displaying all the products from the DB. The weird thing is that, apart for like 5-6 queries, the rest (so around 45) are all this same query:

select * from `roles` where `roles`.`id` = 1 limit 1

An information that might help is that I am using gates to tell what part of the side menu to which user to show, and I am using events to load the componenst of the side menu, since I think it is the only way to pass dynamic data with adminlte.

0 likes
7 replies
okusax's avatar

hi @kris01 you need to identify where that query is fired, I believe that you fogot to use eager loading to load your models in some place inside a loop like a foreach, so every time the loop iterates the query is executed.

If you have telescope or a debugbar you should be able to find it.

1 like
Kris01's avatar

@okusax Thanks to Debugbar I was able to see that those models are all called inside the authServiceProvider.php where I define my gates. But in no place I am calling this model inside a loop. I don't understead !

Snapey's avatar

looks like when you loop over the products, you are checking if the user is allowed to do something with that product (using something like @can)

You should determine the user's role once and then refer to that value in the loop, not keep checking the role

Kris01's avatar

@Snapey Hi, I am using 'can' with adminlte, and I am loading the side elements inside eventsServiceProvider.php . But when I loop the products I am not checking the role, so probably as you said it's because of the 'can' . But what can I do other than using the 'can' ?

Kris01's avatar

@Snapey OK, so those models are all called inside the authService provider where I put my gates. The way I am creating the gates is the following:

public function boot()
    {
        $this->registerPolicies();


        Gate::define('admin_aviable', function ($user) {
          
            if ($user->role()->first()->role == 'admin'){
                return true;
            }
 
            return false;
 
        });

        Gate::define('customer_aviable', function ($user) {

            if ($user->role()->first()->role == 'customer'){
                return true;
            }
 
            return false;
 
        });
    }
Snapey's avatar

Use caching to remove the query every time you check, or eager load the user's role

eager loading;

        Gate::define('admin_aviable', function ($user) {
            $user->loadMissing('role');
            return $user->role->first()->role == 'admin';
        });

note that I removed the braces after role() which will cause a database query every time.

You also seem to have the wrong relationship. Your code suggests that a user can only have one role, but your role method is returning a collection, so you are having to use first(). If your role method on user model was instead hasOne then you would not need to use first().

1 like
Kris01's avatar

@Snapey I did this but it seems that nothing has changed? Is it possible that this is the problem?

public function boot()
    {
        Event::listen(BuildingMenu::class, function (BuildingMenu $event) {
            
            //ADMIN
            $event->menu->addBefore('products',[
                'text' => 'Dashboard',
                'url' => route('admin.dashboard', app()->getLocale()),
                'icon' => 'fas fa-fw fa-columns',
                'can' => 'admin_aviable',
            ]);

            //products
            $event->menu->addIn('products',[
                'text' => 'All Products',
                'url' => route('admin.products', app()->getLocale()),
            ]);
            $event->menu->addIn('products',[
                'text' => 'Pending Products',
                'url' => route('admin.pending.products', app()->getLocale()),
            ]);
            $event->menu->addIn('products',[
                'text' => 'Add Products',
                'url' => route('add.product.view', app()->getLocale()),
            ]);

            //customers
            $event->menu->addAfter('products',[
                'key' => 'customer',
                'text' => 'Customers',
                'url' => route('admin.customers', app()->getLocale()),
                'icon' => 'fas fa-fw fa-users',
                'can' => 'admin_aviable',
            ]);

            //orders
            $event->menu->addAfter('customer',[
                'text' => 'Orders',
                'url' => route('orders', app()->getLocale()),
                'icon' => 'fas fa-fw fa-envelope',
                'can' => 'admin_aviable',
            ]);

            //categories
            $event->menu->addIn('categories',[
                'text' => 'All Categories',
                'url' => route('admin.categories', app()->getLocale()),
            ],
            [
                'text' => 'Add Category',
                'url' => route('admin.add.category', app()->getLocale()),
            ]);


            //sections manager
            $event->menu->addAfter('categories', [
                'key' => 'manager',
                'header' => 'Manager',
            ]);
            $event->menu->addAfter('manager',[
                'text' => 'First Section',
                'url'  => route('admin.first.section.manager.view', app()->getLocale()),
                'icon' => 'fas fa-fw fa fa-puzzle-piece',
                'can' => 'admin_aviable'
            ],
            [
                'text' => 'Second Section',
                'url'  => route('admin.second.section.manager.view', app()->getLocale()),
                'icon' => 'fas fa-fw fa fa-puzzle-piece',
                'can' => 'admin_aviable'
            ],
            [
                'text' => 'Third Section',
                'url'  => route('admin.third.section.manager.view', app()->getLocale()),
                'icon' => 'fas fa-fw fa fa-puzzle-piece',
                'can' => 'admin_aviable'
            ],
            [
                'text' => 'Fourth Section',
                'url'  => route('admin.fourth.section.manager.view', app()->getLocale()),
                'icon' => 'fas fa-fw fa fa-puzzle-piece',
                'can' => 'admin_aviable'
            ]);

        });
    }

because whenever I go inside the admin the model calls to the Role model are absurde!

Please or to participate in this conversation.