2

Laravel 5.2 application.

I have a function in my controller that queries my DB with an order by clause to which I then paginate.

public function foo() {

  $results = DB::orderBy('name', 'desc')->paginate(15);
  return view('index', ['results' => $results]);

}

index.blade.php

@foreach ($results as $result)
 <tr>
  <td>{{ //current $result position is what I need }}</td>
  <td>{{ $result->name }}<td>
  //etc...
 </tr>
@endforeach

How could I output the current row order iteration?

e.g.

Rank | name | //page 1
1      bob  
2      john
3      katie
Rank | name | //page 2
4      dave  
5      michael
6      ruben

Usually I would just output the id but since I have an orderBy clause, the ID won't correctly reflect the current rank. And if I do something like a for() loop then when I go to the next page, it will reset the first result on the second page, to rank 1, even though it's the next page.

Does Laravel have a specific way of achieving this? I've looked through the docs and I'm unable to find what I'm looking for.

6 Answers 6

9

I was able to do this more elegantly than the suggestions provided via the firstItem() method.

public function foo() {

  $results = DB::orderBy('name', 'desc')->paginate(15);

  $rank = $results->firstItem();

  return view('index', ['rank' => $rank, 'players' => $players]);

}


index.blade.php

@foreach ($results as $result)
 <tr>
  <td>{{ $rank++ }}</td>
  <td>{{ $result->name }}<td>
  //etc...
 </tr>
@endforeach
4

You can get a first row number of the current page:

$firstRowOnThisPage = $results->currentPage() * $results->perPage() - $results->perPage() + 1;

And then use this variable in a simple @for loop or just do $i++ in a @foreach loop.

1
  • 1
    If I could I'd upvote this a thousand times, this works brilliantly.
    – JillevdW
    Sep 30, 2018 at 16:49
1

Try something like this:

@foreach ($results as $index => $result)
 <tr>
  <td>{{ (Input::get('page', 1) - 1) * $results->getPerPage() + $index + 1 }}</td>
  <td>{{ $result->name }}<td>
  //etc...
 </tr>
@endforeach

This will only work if your using the ?page= in your url. If that doesn't work you can inject that variable in your controller.

0

I´d give the additional helpers a try:

https://laravel.com/docs/5.1/pagination > Additional Helper Methods

$result->currentPage()

With this method you can surely achieve what you are looking for

0

my solution is

<td>{{$customer->currentPage() * $customer->perPage() - $customer->perPage() + 1 +$loop->index}}</td>

its work for me

0

@foreach ($results as $index => $result) {{ (Input::get('page', 1) - 1) * $results->firstItem() + $index }} {{ $result->name }} //etc... @endforeach

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 20, 2023 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.