Skip to content
Advertisement

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!

Advertisement

Answer

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]);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement