@david001 What are you actually doing in that controller action? You seem to be doing a lot of work just to display some albums in a view. What’s wrong the something like the following?
$albums = Album::latest()->active()->ofType(0)->paginate();
return view('frontend.gallery.album', compact('albums'));
You then have a paginated list of albums that you can iterate over in your view:
@foreach ($albums as $album)
<!-- Display album -->
@endforeach
<!-- Pagination -->
{!! $albums->render() !!}
In the controller action, you’ll see I re-factored your various WHERE clauses into more descriptive query scopes. I also don’t know what “0” refers to in the case of “type”, and the next person reading your code won’t either. Hell, even you might forget what type “0” is when you go back to the code after a period of time!
You want to reduce cognitive load when working with code, so that means getting rid of “magic” numbers that you have to pour through the codebase for to work out what they mean. Consider re-factoring that out into an enum-like class:
class AlbumType
{
const PHOTOS = 0; // Or whatever ‘0’ represents in your application
}
This would make working with type values clearer:
$albums = Album::latest()->active()->ofType(AlbumType::PHOTOS)->paginate();