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

MahmoudAdelAli's avatar

How I Decrase The Queries Counts ?

hello , i have a dashboard and in all my projects i face a issue inside the home page , which the queries is so much to sum and get counts and some thing's like that , so there's any way to shortcut all the queries

public function index(){
        $title = 'Dashboard';
        $clients_count = Client::count();
        $employee_count = Employee::count();
        $latest_employee_count = Employee::latest()->limit(10)->count();
        $latest_tickets_count = Ticket::latest()->limit(10)->count();
        $last_month_tickets_count = Ticket::whereMonth('created_at',Carbon::now()->subMonth()->month)->count();

        return view('backend.dashboard',compact(
            'title','clients_count','employee_count','last_month_tickets_count','latest_employee_count','latest_tickets_count',
        ));
    }

and it

Overall Employees 1
New Tickets
+12.5%
1

Previous Month 0
Leads
-2.8%
4

Previous Month 5
Statistics

Today Leave
4 / 65

Pending Invoice
15 / 92

Completed Tasks
85 / 112

Open Tickets
190 / 212

Closed Tickets
22 / 212
Task Statistics

Total Tasks
385

Overdue Tasks
19
30%
22%
24%
21%
10%

Completed Tasks 166

Inprogress Tasks 115

On Hold Tasks 31

Pending Tasks 47

Review Tasks 5
Today Absent 5
Martin Lewis
4 Sep 2019
Leave Date
Pending
Martin Lewis
4 Sep 2019
Leave Date
Approved
Load More
8.1.734.45ms2MBGET dashboard
10 statements were executed4.39ms

    select * from `users` where `id` = 1 and `users`.`deleted_at` is null limit 1
    1.49ms/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php:53crmt
    select count(*) as aggregate from `clients` where `clients`.`deleted_at` is null
    490μs/app/Http/Controllers/Admin/DashboardController.php:17crmt
    select count(*) as aggregate from `employees`
    390μs/app/Http/Controllers/Admin/DashboardController.php:18crmt
    select count(*) as aggregate from `employees` limit 10
    130μs/app/Http/Controllers/Admin/DashboardController.php:19crmt
    select count(*) as aggregate from `tickets` limit 10
    100μs/app/Http/Controllers/Admin/DashboardController.php:20crmt
    select count(*) as aggregate from `tickets` where month(`created_at`) = '06'
    160μs/app/Http/Controllers/Admin/DashboardController.php:21crmt
    select `name`, `payload` from `settings` where `group` = 'theme'
    400μs/vendor/spatie/laravel-settings/src/SettingsRepositories/DatabaseSettingsRepository.php:28crmt
    select `name`, `payload` from `settings` where `group` = 'company'
    520μs/vendor/spatie/laravel-settings/src/SettingsRepositories/DatabaseSettingsRepository.php:28crmt
    select * from `notifications` where `notifications`.`notifiable_type` = 'App\Models\User' and `notifications`.`notifiable_id` = 1 and `notifications`.`notifiable_id` is not null order by `created_at` desc
    380μsview::6b90a22b47aad2593ad06006e151e17e4f951a38:34crmt
    select * from `notifications` where `notifications`.`notifiable_type` = 'App\Models\User' and `notifications`.`notifiable_id` = 1 and `notifications`.`notifiable_id` is not null and `read_at` is null order by `created_at` desc
0 likes
2 replies
sirch's avatar

you could create 1 query via the query builder to get all the counts. look at unions

Sinnbeck's avatar

If your employee count query is 10 or more, you can just remove this an set it to 10

$latest_employee_count = Employee::latest()->limit(10)->count();

But why are you so concerned? Your query count seems static and should be very fast

Please or to participate in this conversation.