【问题标题】:How to pass multiple array in response - Laravel API如何传递多个数组作为响应 - Laravel API
【发布时间】:2020-04-04 19:22:35
【问题描述】:

我正在 Laravel 中为移动应用程序创建 API。我面临一个问题,我必须将两个数组作为 json 传递,但它显示错误。这是我正在使用的代码

public function showSearchPage($subCatId)
{

$allAdQuery=DB::select('select * from addata where 
fk_adData_subCatData_id=:subId order by adData_date desc, adData_time desc',
["subId"=>$subCatId]);


$allProductQuery=DB::select('select sellerProductData_title, sellerProductData_price,
fk_sellerProductData_subCatData_id,sellerProductData_id from
sellerproductdata where fk_sellerProductData_subCatData_id=:subId
order by sellerProductData_date desc',["subId"=>$subCatId]);

return \Response::json($allAdQuery,$allAdQuery);

}

它告诉我这个错误

Symfony\Component\Debug\Exception\FatalThrowableError (E_RECOVERABLE_ERROR) 参数 2 传递给 Symfony\Component\HttpFoundation\JsonResponse::__construct() 必须是 整数类型,给定数组,在

中调用

知道如何传递两个或多个数组吗?谢谢

【问题讨论】:

  • \Response::json([$allAdQuery,$allAdQuery]);?
  • @u_mulder 感谢它的工作。为什么不将其发布为答案,以便我可以标记它:)

标签: php json laravel api


【解决方案1】:

根据您的数据创建数组:

return \Response::json([
    $allAdQuery,
    $allAdQuery
]);

// with explicit keys:
return \Response::json([
    'key1' => $allAdQuery,
    'key2' => $allAdQuery
]);

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 2021-03-25
    • 2018-02-18
    • 1970-01-01
    • 2021-03-28
    • 2011-02-12
    • 1970-01-01
    • 2021-11-20
    • 2020-11-19
    相关资源
    最近更新 更多