如何设置分页并设置每页显示5个项目的限制

时间:2017-06-27 09:30:15

标签: lumen

如何设置分页并设置每页显示5个项目的限制。当我通过页面没有。它显示该页面项目

class EmployeeController extends Controller{


    public function index(){

        $Employees  = Employees::all();

        $limit=5;
        $itemPerPage=5;
        $response = array();

        $count  = Employees::count();
        $totalPage= ceil($count/$itemPerPage);

        $response['totalPages'] = $totalPage;
        $response['data'] = $Employees;
        return response()->json($response);
    }

2 个答案:

答案 0 :(得分:0)

尝试使用laravel paginate函数获取带分页的结果

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

尝试从laracasts讨论频道

的示例代码

https://laracasts.com/discuss/channels/lumen/pagination-in-lumen?page=1

希望这会有所帮助

答案 1 :(得分:0)

public function index(){

        $Employees  = Employees::all();
        $page = Input::get('page', 1); 


        $itemPerPage=5;
        $response = array();

        $count  = Employees::count();

        $offSet = ($page * $itemPerPage) - $itemPerPage;

        $itemsForCurrentPage = array_slice($Employees->toArray(), $offSet, $itemPerPage, true);

       return new LengthAwarePaginator($itemsForCurrentPage, count($Employees), $itemPerPage, $page);

    }