2017-01-21 92 views
2

我工作的一个API,我被困在分页 首先,我稍后发送基于用户通过限制值仅前10条记录,我应该送next 10条记录Laravel 5.3 API分页

所以我这样做

//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. 

我的事情我不是得到错误牛逼这样做是正确

期待着急需帮助

谢谢

回答

1

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

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

非常感谢你们一如既往的出色,如果数据只有120个,请告诉我如何显示消息'not found',如果他们输入555这样的东西,谢谢 –

+1

@MrRobot just add像'if(count($ drivers)== 0){//没有数据}' –

+0

,我想发送下10个不是最后发送的10个值 –

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

+0

非常感谢你的时间真的很感激它 –