Laravel 5.3 API分页

时间:2017-01-21 16:05:49

标签: php laravel api laravel-5.3 laravel-pagination

我正在使用API​​,而且我陷入了分页状态 首先我应该根据用户传递的限制值,仅发送前10条记录,我应该发送next 10条记录

所以我做了这个

//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()
    ]);
}

但我在请求http://localhost:8000/api/v1/search-list/10

时遇到错误
BadMethodCallException in Macroable.php line 74:
Method select does not exist.

我不能做得对

期待急需的帮助

谢谢

2 个答案:

答案 0 :(得分:1)

您应该使用paginate()方法而不是get()

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

答案 1 :(得分:1)

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

;

之后删除paginate($limit)

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