我想通过
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() 的参数太少,第 30 行在 D:\xampp\htdocs\IDM pp\Api\V1\Controllers\Front\PostController.php 中传递了 0 个参数,并且预期为 1 个参数
那么如何在邮递员中传递参数以及此错误的解决方案是什么?
首先将此行更改为
$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);
在 try 块内调用 allUserPost 函数时缺少参数。
应该是
$allPost = Post::allUserPost($request);
然后您可以从 $request 变量中检索参数。
只需更改代码中的这一行即可
$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;
}
希望这对您有帮助。