gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:36:20 26 4
gpt4 key购买 nike

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

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

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

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

使用上述方法返回空值。任何帮助将不胜感激

对不起各位。我找到了解决方案。我传递的数据已经是对象形式。因此我需要将它转换成一个数组然后传递它。

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

现在它可以工作了!

最佳答案

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

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

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

关于php - 将多个数组返回到 Response::json laravel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21700215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com