Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return multiple array to Response::json laravel?

How we can return multiple array in json. Suppose we get the following response in Laravel eloquent:

$user= User::all();
$post= Post::all();
$comment= Comment:all();

Now I want to return response in json which include these data:

Response::json(array('user'=>$user,'post'=>$post,'comment'=>$comment));

Using the above method empty value is returned. Any help would be appreciated

Sorry guys. I found the solution. The data that I was passing was already in object form. Therefore I needed to convert it into an array and then pass it.

$user= User::all()->toArray();
$post= Post::all()->toArray();
$comment= Comment:all()->toArray();

Now it will work!

like image 272
Anil Sharma Avatar asked Feb 11 '14 11:02

Anil Sharma


1 Answers

i think you can try this method:

$user= User::all()->toArray();
$post= Post::all()->toArray();
$comment= Comment:all()->toArray();

Response::json(['user'=>$user,'post'=>$post,'comment'=>$comment]);
like image 112
DolDurma Avatar answered Oct 04 '22 07:10

DolDurma