首页 > 解决方案 > 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);

标签: laravellaravel-5

解决方案


嗨,您可以使用 laravel 分页器获取当前页面Paginator::currentPageResolver

public function index()
{
    $currentPage = 3; // You can set this to any page you want to paginate to

    // Make sure that you call the static method currentPageResolver()
    // before querying users
    Paginator::currentPageResolver(function () use ($currentPage) {
        return $currentPage;
    });

    $users = \App\User::paginate(5);

    return view('user.index', compact('users'));
}

推荐阅读