Gardelin
Gardelin

Reputation: 1071

Laravel API Pagination ErrorException

I'm working on simple API in Laravel 5 and i want to make pagination because of lot of data in database.

This is method which is called on GET request for route api/v1/words

public function index()
    {
        //$words = Word::all();
        $limit = Input::get('limit') ?: 3;
        $words = Word::paginate($limit);
        //dd(get_class_methods($words));
        return $this->respondWithPagination($words);
    }
                     .
                     .  
                     .

    public function respondWithPagination($words)
    {
        return $this->respond([
            'words' => $this->wordTransformer->transformCollection($words->all()),
            'paginator' => [
                'totalCount' => $words->getTotal(),
                'totalPages' => ceil($words->getTotal() / $words->getPerPage()),
                'currentPage' => $words->getCurrentPage(),
                'limit' => $words->getPerPage(),
                'previousPageUrl' => $words->previousPageUrl(),
                'nextPageUrl' => $words->nextPageUrl()
            ]
        ]);
    }

But my log gives me this error:

[2015-04-17 09:50:28] local.ERROR: exception 'ErrorException' with message 'call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\Eloquent\Collection' does not have a method 'getTotal'' in /home/vagrant/Code/zadarplus2/vendor/laravel/framework/src/Illuminate/Pagination/AbstractPaginator.php:463
    Stack trace:
    #0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'call_user_func_...', '/home/vagrant/C...', 463, Array)
    #1 /home/vagrant/Code/zadarplus2/vendor/laravel/framework/src/Illuminate/Pagination/AbstractPaginator.php(463): call_user_func_array(Array, Array)
    #2 /home/vagrant/Code/zadarplus2/app/Http/Controllers/WordController.php(90): Illuminate\Pagination\AbstractPaginator->__call('getTotal', Array)
    #3 /home/vagrant/Code/zadarplus2/app/Http/Controllers/WordController.php(90): Illuminate\Pagination\LengthAwarePaginator->getTotal()
    #4 /home/vagrant/Code/zadarplus2/app/Http/Controllers/WordController.php(43): App\Http\Controllers\WordController->respondWithPagination(Object(Illuminate\Pagination\LengthAwarePaginator))
    #5 [internal function]: App\Http\Controllers\WordController->index()
    #6 /home/vagrant/Code/zadarplus2/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(246): call_user_func_array(Array, Array)

What am I doing wrong?

Upvotes: 1

Views: 858

Answers (1)

menjaraz
menjaraz

Reputation: 7575

$words is an Illuminate\Pagination\Paginator object (see documentation here).

You have called a method which doesn't belong

  • neither to the Paginator class
  • nor to its underlying Collection accessed through dynamic call (see also documentation here).

Explore/study thoroughly the documentations to know the correct spelling of the methods you need.


Hints:

  • count(): Get the number of items for the (Paginator) current page (reference).
  • count(): Count the number of items in the (underlying) Collection (reference).

Upvotes: 1

Related Questions