Showing 1 to 10 of 35 entries not working in laravel

1.7k Views Asked by At

I'm working with laravel 5.6 showing data, but it's not working perfectly.

This code displayed me in fist page Showing 0 to 10 of 35 and last page displayed Showing 30 to 40 of 35

$page    = $request->has('page') ? $request->get('page') : 1;
$total   = UserAdmin::count();
$perPage = 10;
$showingTotal  = $page * $perPage;

$currentShowing = $showingTotal>$total ? $total : $showingTotal;
$showingStarted = $showingTotal - $perPage;
$tableInfo = "Showing $showingStarted to $showingTotal of $total";

I want to Showing 1 to 10 of 35 entries in first page and last page will be display Showing 30 to 35 of 35 entries

4

There are 4 best solutions below

0
Daart Kote On BEST ANSWER
$tableInfo = "Showing $showingStarted to $currentShowing of $total";

Use specified $showingTotal instead of $currentShowing

0
AddWeb Solution Pvt Ltd On

you should try this:

Please use paginate in your query and try like:

$perPage = 10;

$rsltUsers   = UserAdmin::paginate($perPage);
0
Matadeleo On

This is because your $showingTotal is a fixed value, calculated from $page * $perPage.

A quick and dirty solution would be to add a line:

if ($showingTotal > $total) {
    $showingTotal = $total;
}

But please, consider using the proper paginate offered in Laravel. The $showingTotal should be dynamically updated, not simply calculated from fixed variables.

0
Jhoan Yañez Forero On

why so complex logic when it can be obtain by direct object members $shops->firstItem() and $shops->lastItem()

Also, in case of total 9 records it will show wrong in last page Showing 6 to 10 of total 9 entries

example: Showing {{ $shops->firstItem() }} to {{ $shops->lastItem() }} of total {{$shops->total()}} entries