Ad

How To Pass Multiple Array In Response - Laravel API

- 1 answer

I am creating my API for mobile app in Laravel. I am facing an issue where I have to pass two arrays as json but it shows error. Here is the code which I am using

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

}

and it shows me this error

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Argument 2 passed to Symfony\Component\HttpFoundation\JsonResponse::__construct() must be of the type integer, array given, called in

Any idea how can I pass two or multiple arrays? Thanks

Ad

Answer

Create array from your data as:

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

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