boneill81
7 months ago

Retrieve all items using pagination

Posted 7 months ago by boneill81

Hi Guys, I have a useful and clean search method which builds up an eloquent query based on attributes that are sent to it. At the minute it uses get() and works fine. I now want to add in pagination which again works. However at the minute I am doing the following:

if (!empty($parameters['page'])) {
    $results = $query->with('customAttributes')->orderBy('created_at', 'desc')->paginate();
}else{
    $results = $query->with('customAttributes')->orderBy('created_at', 'desc')->get();
}

I was wondering if there is not perhaps a nicer built in way to do this. For example pass false to paginate in which case it returns all records as per a normal get() which it executes internally anyway.

Thanks

Please sign in or create an account to participate in this conversation.