0

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

Venantius
  • 2,471
  • 2
  • 28
  • 36
Mahbub
  • 49
  • 1
  • 1
  • 11
  • use paginate : https://laravel.com/docs/5.6/pagination – Saurabh Mistry May 22 '18 at 10:06
  • What is your expected result? – Matadeleo May 22 '18 at 10:06
  • 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. But it displayed Showing 0 to 10 of 35 and last page displayed Showing 30 to 40 of 35 @Matadeleo – Mahbub May 22 '18 at 10:08
  • You're expectation is incorrect. The last page should display 31 to 35. (Each page range would be 1 to 10, 11 to 20, 21 to 30, 31 to 35) – RichardAtHome May 22 '18 at 10:11
  • Yeah, my expectation is incorrect. I understand my problem. please can you help me how to displayed this 1 to 10, 11 to 20, 21 to 30, 31 to 35. @RichardAtHome – Mahbub May 22 '18 at 10:20
  • $firstNumber = $currentPage * $pageSize - ($pageSize - 1); $lastNumber = $currentPage * $pageSize; if ($lastNumber > $totalNumber) {$lastNumber = $totalNumber; } echo "Showing $startNumber to $lastNumber of $totalNumber; (typed into SO without testing, but you get the idea) :-) – RichardAtHome May 22 '18 at 11:42

4 Answers4

3

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

OMR
  • 11,736
  • 5
  • 20
  • 35
1

you should try this:

Please use paginate in your query and try like:

$perPage = 10;

$rsltUsers   = UserAdmin::paginate($perPage);
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
1
$tableInfo = "Showing $showingStarted to $currentShowing of $total";

Use specified $showingTotal instead of $currentShowing

Daart Kote
  • 81
  • 5
0

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.

Matadeleo
  • 504
  • 8
  • 18