Mahbub
Mahbub

Reputation: 49

Showing 1 to 10 of 35 entries not working in laravel

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

Upvotes: 0

Views: 1958

Answers (4)

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

Upvotes: 3

Daart Kote
Daart Kote

Reputation: 81

$tableInfo = "Showing $showingStarted to $currentShowing of $total";

Use specified $showingTotal instead of $currentShowing

Upvotes: 1

Matadeleo
Matadeleo

Reputation: 514

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.

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

you should try this:

Please use paginate in your query and try like:

$perPage = 10;

$rsltUsers   = UserAdmin::paginate($perPage);

Upvotes: 1

Related Questions