简体   繁体   中英

Laravel 5.3 API Pagination

i'm working on an API, and i'm stuck at pagination first i should send only first 10 records later based on the limit value passed by user i should send the next 10 records

so for i did this

//search Drivers
public function getSearchList($limit) 
{
    //dd($limit);
    $drivers = Driver::paginate($limit)
               ->select('id','first_name','last_name','phone_number','registration_id')
               ->orderBy('first_name', 'asc')
               ->get();

    return Response::json([
        'data' => $drivers->all()
    ]);
}

but i'm getting error on requesting http://localhost:8000/api/v1/search-list/10

BadMethodCallException in Macroable.php line 74:
Method select does not exist.

i thing i'm not doing it right

looking forward for much needed help

thank you

You should use paginate() method instead of get() :

$drivers = Driver::select('id', 'first_name', 'last_name', 'phone_number', 'registration_id')
           ->orderBy('first_name', 'asc')
           ->paginate($limit);
$drivers = Driver::
          select('id','first_name','last_name','phone_number','registration_id')
           ->orderBy('first_name', 'asc')
           ->paginate($limit);

Delete ; after paginate($limit)

https://laravel.com/docs/5.3/pagination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM