Laravel:带有分页参数的API响应

时间:2018-05-07 06:17:31

标签: laravel laravel-5.5 dingo-api

我想通过pagination parameters通过POSTMAN并在sort,order,limits中传递model以获取分页符查询。我怎样才能做到这一点?目前它返回错误。

目前我的路线: http://localhost:8000/api/allpost

我的PostController功能:

 public function index(Request $request)
{
    try {
        $allPost = Post::allUserPost();
        if($allPost !="" && count($allPost)>0) {
           return  [
                'status_code'     =>     200,
                'message'         =>     "Post retrieved successfully",
                'PostDetails'     =>     $allPost,
            ];  
        } else {
            return response()->json([
                'message'       => "Post data not found",
                'status_code'   => 403,
            ]);
        }
    } catch (\Exception $ex) {
         return response()->json([
            'message'       => "Internal server error",
            'status_code'   => 500,
        ]);
    }
}

我的POST模型函数:

 public static function allUserPost(Request $request){

    $sort = $this->parameters->sort();
    $order = $this->parameters->order();
    $limit = $this->parameters->limit();

    $userPost       =  Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit)->get();
                    $userPost_array   = $userPost->toArray();
                    foreach ($userPost_array as $key => $value) {
                        # code...
                        $attributes_arr             =   array_column($userPost_array[$key]['categories'], 'attribute_id');
                        $category_ids               =   Attribute::whereIn("id",$attributes_arr)->pluck('category_id');
                        $category_ids               =   array_unique($category_ids->toArray());
                        $category_details_with_att  =   Post::getCategoryWithAttributeData($attributes_arr,$category_ids);
                        unset($userPost_array[$key]["categories"]);
                        $userPost_array[$key]["categories"] = $category_details_with_att->toArray();
                    }
        return  $userPost_array; 
}

目前它返回错误

  

类型错误:函数App \ Post :: allUserPost()的参数太少,0在第30行的D:\ xampp \ htdocs \ IDM \ app \ Api \ V1 \ Controllers \ Front \ PostController.php中传递1预期

那么如何在邮递员中传递参数,以及这个错误的解决方案是什么?

3 个答案:

答案 0 :(得分:0)

在try块中调用allUserPost函数时,你缺少一个参数。

应该是

match

然后您可以从$ request变量中检索参数。

答案 1 :(得分:0)

首先将此行更改为$allPost = Post::allUserPost();

$allPost = Post::allUserPost($request);

然后更改此代码

$sort = $this->parameters->sort();
$order = $this->parameters->order();
$limit = $this->parameters->limit();

$sort = $request->sort;
$order = $request->order;
$limit  = $request->limit;

然后你可以在像

这样的查询字符串中传递这些参数
http://localhost:8000/api/allpost?sort=somesort&order=asc&limit=10

同样变这一行

$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit)->get();

$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit);

答案 2 :(得分:0)

只需在代码中更改此行

即可
$allPost = Post::allUserPost($request);

然后在您的函数中,您必须更改您的请求类型。之后,您只需使用paginate()方法而不是get()方法进行一次更改。

public static function allUserPost(Request $request){

    $sort = $request->sort;
    $order = $request->order;
    $limit = $request->limit;

    $userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit);
    $userPost_array   = $userPost->toArray();
    foreach ($userPost_array as $key => $value) {
    $attributes_arr =   array_column($userPost_array[$key]['categories'], 'attribute_id');
    $category_ids = Attribute::whereIn("id",$attributes_arr)->pluck('category_id');
    $category_ids = array_unique($category_ids->toArray());
    $category_details_with_att = Post::getCategoryWithAttributeData($attributes_arr,$category_ids);
                        unset($userPost_array[$key]["categories"]);
                        $userPost_array[$key]["categories"] = $category_details_with_att->toArray();
    }
      return  $userPost_array; 
}

我希望这会对你有所帮助。