phplaravellaravel-5pagination

get page number in object of laravel


i have object given below and i wanted to pagination in this how can i get

$productListArray = array();
 $productListObject = ((object)[
                "id"=>$productList->id, 
                "title"=>$productList->title,
                "slug"=>$productList->slug,
                'categoryName'=>$categoryName[0]->cat_title,
                'brand'=>$brandName[0]->brandname,
                'minMrp'=>$minMrp,
                'maxMrp' =>$maxMrp,
                'minSellingPrice' => $minSellingPrice,
                'maxSellingPrice' => $maxSellingPrice,
                'rating'=>$productList->rating,
                'rating_count' => $productList->rating_count,
                'image' => $img[0]
            ])->paginate();
       array_push($productListArray, $productListObject);
    }
    return response()->json($productListArray, 200);

Solution

  • thanks for your support i found solution, first add on your header

    use Illuminate\Pagination\Paginator;
    use Illuminate\Support\Collection; 
    use Illuminate\Pagination\LengthAwarePaginator;
    

    then make an other function

    public function paginate($items, $perPage = 5, $page = null, $options = [])
    {
        $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
        $items = $items instanceof Collection ? $items : Collection::make($items);
        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
    }
    

    after than call this function where you want in class

    $data = $this->paginate($productListArray);
        return response()->json($data, 200);  
    

    last 2 lines already mentioned in my questions thanks again