There would be cases where you want to pass a paginated collection to a view, but if the collection is empty maybe you want to return another view.
If you are on a blade file you can handle this by using a "forelse" method but if you want to return different view if the collection is empty?
Laravel provide us a simple method to validate if this collection is empty, but first lets see another approach:
Maybe you were tempt to use the total method to make something like this:
$results = User::paginate();
if($results->total() > 0) {
return view('users.show', compact('results'));
}
return view('anotherview');
an easiest and more readable way to handle this is with the method "hasPages()":
$results = User::paginate();
if($results->hasPages()) {
return view('users.show', compact('results'));
}
return view('anotherview');
Top comments (0)