Try a lengthaware paginator and check results, see:
https://laracasts.com/discuss/channels/guides/length-aware-paginator
I have a laravel 5.4.27 application hosted on a linux server that registers owners and their pets, and I'm using laravel's pagination to show the results in groups of 20, but I've been getting this "random" issue on the last page every now and then.
There are currently 175 pets registered, using pagination there are a total of 9 pages, the last of which (page 9) cannot be seen at the moment, it simply displays the "Whoops, looks like something went wrong" message. I said the problem was "random" because when there were only 8 pages available, the last page (8) could be seen with no issues. Back when there were less entries and page 5 was the last one, the same issue happened: page 5 couldn't be seen but it worked like a charm once page 6 became available, and page 6 never had that issue.
I did a google search to see if this was an issue with pagination but didn't find anything similar to the problem above. I haven't made any changes to the index.blade (where the entries are shown) or the controller to explain why the problem happens at times, so I'm not sure what the issue could be.
Looking at the log file, this is what I get:
Next ErrorException: Trying to get property of non-object (View: /resources/views/pets/index.blade.php) in /storage/framework/views/1d49586136df453a0e78daeff32f71158a41fcb5.php:40
[2017-08-11 15:50:17] local.ERROR: ErrorException: Trying to get property of non-object in /storage/framework/views/1d49586136df453a0e78daeff32f71158a41fcb5.php:40
Both entries are followed by a stack trace of 47 lines each (which I've excluded to keep this short), but most are for Illuminate/Routing, Illuminate/Pipeline and Illuminate/View.
My controller:
public function index()
{
$pets = Pet::with('owner')->paginate(20);
return view('pets.index', compact('pets'));
}
My index view:
@if ( !$pets->count() )
No registered pets.
@else
<div class="table-responsive">
<table class="table table-striped">
<thead><tr class="headings">[...]</tr></thead>
<tbody>
<?php $current = 1; ?>
@foreach( $pets as $pet )
<tr class="">
<td>{{ $pets->perPage()*($pets->currentPage()-1)+$current }}</td>
<td>{{ ucfirst(mb_strtolower($pet->name)) }}</td>
<td>{{ ucfirst($pet->breed) }}</td>
<td>{{ ucwords(mb_strtolower($pet->owner->name)) }}</td>
<td>{{ $pet->owner->phone }} @if($pet->owner->phone != '' && $pet->owner->cellphone != '') / @endif {{ $pet->owner->cellphone }}</td>
<td><a href="{{ route('pets.show', $pet->num) }}">View</a></td>
</tr>
<?php $current++; ?>
@endforeach
</tbody>
</table>
{{ $pets->links() }}
</div>
@endif
Thanks in advance for any help with this!
Please or to participate in this conversation.