user4748790
user4748790

Reputation:

Laravel won't let me save pagination variable in an array

I need to return a json with all products and stores.

I tried this

    $products = $products->paginate(20);
    $stores=$this->getStores();
    return ['product'=> $products, 'stores' =>$stores];

but $products returns empty. (If i run "return $products" it works fine)

Is there something i can do? Why laravel doesn't let me have the pagination in an array?

Upvotes: 1

Views: 103

Answers (2)

user4748790
user4748790

Reputation:

Solution:

return ['product'=> $products->toArray(), 'stores' =>$stores];

or

return Response::json(
  array(
    'products' => $products->toArray(),
    'stores' => $stores,
  ), 200
);

Upvotes: 1

Flm
Flm

Reputation: 261

You could do something like this :

return Response::json(
  array(
    'products' => $products,
    'stores' => $stores,
  ), 200
);

You also need to 'use' the Response facade: use Illuminate\Support\Facades\Response;

Upvotes: 3

Related Questions