32

I am trying to paginate Model result, but I am getting "Method paginate does not exist.". Here is my code:

$user_dispatches = Dispatch::all()->where('user_id', Auth::id())->paginate(10);

I need to get all records where users id equals current authenticated users id. Works well without paginate() method.

Andrii H.
  • 1,682
  • 3
  • 20
  • 40

9 Answers9

107

Extending a bit Alexey's perfect answer :

Dispatch::all() => Returns a Collection

Dispatch::all()->where() => Returns a Collection

Dispatch::where() => Returns a Query

Dispatch::where()->get() => Returns a Collection

Dispatch::where()->get()->where() => Returns a Collection

You can only invoke "paginate" on a Query, not on a Collection.

And yes, it is totally confusing to have a where function for both Queries and Collections, working as close as they do, but it is what it is.

Amarnasan
  • 14,939
  • 5
  • 33
  • 37
54

You need to remove all():

Dispatch::where('user_id', Auth::id())->paginate(10);

When you're using all() you get all the rows from the table and get a collection. Then you're using collection method where() (and not Query Builder method where()) and then you're trying to use paginate() method on the collection and it doesn't exist.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
16

for use all recorde and pagination , you need use below code :

$user_dispatches = Disspath::paginate(8);
Mostafa Norzade
  • 1,578
  • 5
  • 24
  • 40
7

You need remove method all() :

$user_dispatches = Dispatch::where('user_id', Auth::id())->paginate(10);

Because all() return a Collection while paginate() used a Builder

Mostafa Norzade
  • 1,578
  • 5
  • 24
  • 40
Joan Nguyen
  • 372
  • 1
  • 5
5

The method toQuery() changes a collection to query:

$pacientes = Paciente::get()->toQuery()->paginate(20);
SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
Javier Duque
  • 51
  • 1
  • 1
2
Dispatch::where('user_id', auth()->user()->id)->paginate(10);
parastoo
  • 2,211
  • 2
  • 21
  • 38
1

You can create own custom class:

<?php
namespace App\CustomClasses;

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

class ColectionPaginate
{
    public static function paginate(Collection $results, $pageSize)
    {
        $page = Paginator::resolveCurrentPage('page');
        
        $total = $results->count();

        return self::paginator($results->forPage($page, $pageSize), $total, $pageSize, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => 'page',
        ]);

    }

    /**
     * Create a new length-aware paginator instance.
     *
     * @param  \Illuminate\Support\Collection  $items
     * @param  int  $total
     * @param  int  $perPage
     * @param  int  $currentPage
     * @param  array  $options
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    protected static function paginator($items, $total, $perPage, $currentPage, $options)
    {
        return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
            'items', 'total', 'perPage', 'currentPage', 'options'
        ));
    }
}

and then use it:

use App\CustomClasses\ColectionPaginate;
...
$result = $query->limit(100)->get();
$paginatedResult = ColectionPaginate::paginate($result, 10);
Azek
  • 27
  • 3
0

**Solved To Clarify the solution from above
Change...

$user_dispatches = Dispatch::all()->where('user_id', Auth::id())->paginate(10);

to

$user_dispatches = Dispatch::where('user_id', Auth::id())->paginate(10)

In another project I was attempting to return a view with my posts array and I was also able to paginate like this...In PostController

public function index()
{
  $posts = Post::where('user_id', Auth::id());
  return view('admin.posts.index', ['posts'=>$posts->paginate(5)]);
}
launghe
  • 11
  • 3
0

If working on laravel + Blade there's a case when I need to keep the all() or get() what I do is to add a toQuery() function to make it work. In my case, this snippet of code helped me get the pagination on Blade without changing my index function:

public function index()
  {
    $users = User::all();
    foreach ($users as $user) {
      //some code I need
      }
    }

    return view('users.index', ['users' => $users->toQuery()->paginate(5)]);
  }