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

Inquisitive's avatar

Bet way to retrieve count of total rows with and without limit

I am doing something like:

$rows = DB('table_name')
        ->where..... 
        ->where.....
        .....
        .....
        ....
        ->offset(0)
        ->limit(50)
        ->get()

I am integrating this with jquery serverside datatable. So, instead of repeating this whole code as follows to get count. Is there any other best way, so that I can stop myself from repeating things. As this where clasue has a very big list.

$count= DB('table_name')
        ->where..... 
        ->where.....
        .....
        .....
        ....
        ->count();
0 likes
2 replies
Nash's avatar
Nash
Best Answer
Level 20

You could do something like

$query = DB::('table_name')
    ->where(...)
    ->where(...);

$count = $query->count();

$data = $query->offset(0)->limit(50)->get();
NOMGUY's avatar

You could simply go to your blade file and write,

{{ count($column_name) }}

Please or to participate in this conversation.