将多个数组返回到Response :: json laravel?

Ani*_*rma 5 php arrays json laravel-4

我们如何在json中返回多个数组.假设我们在Laravel eloquent中得到以下响应:

$user= User::all();
$post= Post::all();
$comment= Comment:all();
Run Code Online (Sandbox Code Playgroud)

现在我想在json中返回包含这些数据的响应:

Response::json(array('user'=>$user,'post'=>$post,'comment'=>$comment));
Run Code Online (Sandbox Code Playgroud)

使用上面的方法返回空值.任何帮助,将不胜感激

对不起大家.我找到了解决方案.我传递的数据已经是对象形式.因此我需要将其转换为数组然后传递它.

$user= User::all()->toArray();
$post= Post::all()->toArray();
$comment= Comment:all()->toArray();
Run Code Online (Sandbox Code Playgroud)

现在它会工作!

Dol*_*rma 17

我想你可以尝试这种方法:

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

Response::json(array('user'=>$user,'post'=>$post,'comment'=>$comment));
Run Code Online (Sandbox Code Playgroud)