Search code examples
phplaravel-5collectionspaginationpluck

Laravel5: How to access paginated data before passing it to blade


I fetch some data with

$merchants = Merchant::selectRaw($query)->with(...)->whereHas(...)->where(...)->paginate(10);

Now, I want to pluck() this data before passing it to blade. This doesn't work:

$collection = collect($merchants);

When I fetch the data with

$merchants = Merchant::selectRaw($query)->with(...)->whereHas(...)->where(...)->get();

I can use $collection = collect($merchants); without any problemns, but pagination does not work for sure.

So, how can I combine paginate() with collect() or rahter access paginated data in the controller before passing it to blade?


Solution

  • You can get the collection using

    $collection = $merchants->getCollection();
    

    or get data as an array using

    $array = $merchants->items();