Laravel:对API的限制和页码进行分页

时间:2018-05-09 05:59:30

标签: laravel laravel-5.5

我有一个查询,在哪里可以从相关表中获取所有数据并得到总10 rows。现在我想在我的查询中设置分页参数。如果我的pagenumber=1limit=2返回1 and 2 rows数据与page=1limit=2,如果我发送page-3limit-2返回5 and 6 rowspage-3。如果page=nulllimit=null返回所有数据。

我该怎么办呢。

我的功能:

Post::with(['product.categories.attributes'])->whereStatus("Active")->get();

另外我如何在POSTMAN

中传递此内容

1 个答案:

答案 0 :(得分:2)

您需要的只是跳过/接受

https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset

您的代码类似于:

public function show(Request $request) {
    $perPage = $request->perpage;

    if ($request->page == "") {
        $skip = 0;
    else {
        $skip = $perPage * $request->page;
    }

    $result = Post::with(['product.categories.attributes'])
        ->skip($skip)
        ->take($perPage)
        ->where("Status", "Active")
        ->get();
}

未设置$ _POST [' perpage']的情况可以在这里探讨:

Skip and take all?

相关问题