简体   繁体   English

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

[英]Return multiple array to Response::json laravel?

How we can return multiple array in json.我们如何在 json 中返回多个数组。 Suppose we get the following response in Laravel eloquent:假设我们在 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!现在它会起作用了!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM