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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21700215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:23:19  来源:igfitidea点击:

Return multiple array to Response::json laravel?

phparraysjsonlaravel-4

提问by Anil Sharma

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

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

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

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

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

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!

现在它会起作用了!

回答by DolDurma

i think you can try this method:

我想你可以试试这个方法:

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

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