Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

While working once with the Laravel eloquent, I have tried to get the user’s record. Where as it works without the paginate() method but with using it, throws error using the all() and get() method. On the other hand it is working well with where() method of the eloquent. While working with the where() all the time unnecessarily with some unused conditions is not seems good, if I need all the records and paginate them.

Here is what, I have tried

use App\Models\User;

$users = User::all()->paginate(10);

The Error, I have got.

BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

So, I dig over internet and found the following reasons of the what it exactly need to work with the all() method.

Summery:

User::all() => Returns Collection in response

User::all()->where() => Returns Collection in response

User::where() => Returns Query in response

User::where()->get() => Returns Collection in response

The default Laravel Paginate method has been developed to work with the Query response but not with the collection return.  This means the following conditions with not work with the paginate(), such as:

User::all()->paginate(10); Not
User::all()->where()->paginate(10); Not
User::where()->get()->paginate(10); Not
User::where()->paginate(10); Yes

If you are working with the all() and get() for fetching the eloquent results, it will not going to work. you have to try with using paginate() by removing all() and get() from your query. But in some conditions you will need to work with the custom resulted response format like array to collection format, In such cases the pagination will not also work. So, to get rid of this situation, we have a method which is below:

use App\Models\User;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;


/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(Request $request)
{
    $per_page = $request->per_page ? $request->per_page : 20;

    return view('users.index', [
        'users' => $this->ArrToCollection(User::all(),$per_page)
    ]);
}

/**
 * function to convert array response to the paginated collection response
 *
 * @var collection
 */
protected function ArrToCollection($items, $perPage = 10, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

Now, by using this way, you will find your code starts working well as you desired.

You can get more details about the Laravel custom pagination topic, you can check this link here.
To get to know more about other Laravel topics, you can check these articles too.

Please follow and like us:

Related Posts

Leave a Reply

Share