First of all you should be getting the results from the database with pagination not putting everything into an array first.
Dec 6, 2015
5
Level 12
Return pagination in 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];
and this
return Response::json(
array(
'products' => $products,
'stores' => $stores,
), 200
);
but $products returns empty. (If i run "dd($products)" i can see the results)
Is there something i can do? Why laravel doesn't let me have the pagination in an array?
Level 41
@Chrysanthos Use either the toArray or toJson methods, depending on what you need. For example:
$products = $products->paginate(20)->toArray();
return [
'products' => $products,
'stores'=> $stores
];
2 likes
Please or to participate in this conversation.