1

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?

Alley Shairu
  • 1,234
  • 1
  • 7
  • 11
Javed
  • 817
  • 4
  • 22
  • 44

3 Answers3

1

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);
Amit Bisht
  • 329
  • 3
  • 15
  • 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` – Javed May 07 '18 at 06:34
  • 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 – Amit Bisht May 07 '18 at 06:44
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.

Alley Shairu
  • 1,234
  • 1
  • 7
  • 11
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.

Chirag Patel
  • 1,545
  • 2
  • 9
  • 17