2

I want to pass pagination parameters through POSTMAN and pass sort,order,limits in my model to get query with paginate.? how can i do this? Currently it return error.

Currently my route : http://localhost:8000/api/allpost

My PostController function :

 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,
        ]);
    }
}

And my POST model function :

 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; 
}

Currently it returns error

Type error: Too few arguments to function App\Post::allUserPost(), 0 passed in D:\xampp\htdocs\IDM\app\Api\V1\Controllers\Front\PostController.php on line 30 and exactly 1 expected

So how can i pass parameters in postmen and whats the solution for this error?

3 Answers 3

2

First change this line to $allPost = Post::allUserPost();

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

and then change this code

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

To

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

and then you can pass these paramets in a query string like

http://localhost:8000/api/allpost?sort=somesort&order=asc&limit=10

Also chage this line

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

to

$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit);
Sign up to request clarification or add additional context in comments.

2 Comments

when i pass only limits and set query for paginate($limit) it return error Type error: Too few arguments to function Illuminate\\Support\\Collection::get(), 0 passed in D:\\xampp\\htdocs\\IDM\\vendor\\laravel\\framework\\src\\Illuminate\\Pagination\\AbstractPaginator.php on line 577 and at least 1 expected
change this line $userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit)->get(); to $userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit); in allUserPost() method
0

You are missing an argument when calling the allUserPost function inside the try block.

It should be

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

and then you can retrieve the parameters from the $request variable.

Comments

0

Just change this line in your code

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

And then in your function, you have to change your request type. And after that you have to do one more change only use paginate() method not with get() method.

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; 
}

I hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.