Anil Sharma
Anil Sharma

Reputation: 2962

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!

Upvotes: 6

Views: 49198

Answers (1)

DolDurma
DolDurma

Reputation: 17303

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]);

Upvotes: 21

Related Questions