Javed
Javed

Reputation: 857

Laravel : API response with pagination parameter

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?

Upvotes: 2

Views: 8860

Answers (3)

Amit Bisht
Amit Bisht

Reputation: 339

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

Upvotes: 2

Chirag Patel
Chirag Patel

Reputation: 1642

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.

Upvotes: 0

Sher Afgan
Sher Afgan

Reputation: 1234

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.

Upvotes: 0

Related Questions