【发布时间】:2020-11-20 09:18:00
【问题描述】:
嘿,我的堆栈溢出书呆子,我需要一些帮助,不确定我是否刚刚遇到心理障碍,但我遇到了一些问题,我返回查询时想知道是否有人可以帮助我...
这是我的控制器
{
$shopperId = auth()->guard('shoppers')->user()->id;
$completedItems = $this->shopifyCartService->getCompleteItems($shopperId);
if ($completedItems) {
return $this->responseHelper->response(true, "All completed items", $completedItems, 200);
}
return $this->responseHelper->response(false, "No completed item / items", null, 401);
}
这是我的回复助手
public function response($successful, $message, $data, $code)
{
return response()->json(['data' =>
[
'successful' => $successful,
'message' => $message,
'data' => $data,
]
], $code);
}
这是我的雄辩的查询
/**
* @param int $shopperId
* @return mixed
*/
public function getCompleteItems(int $shopperId)
{
return ShopifyCart::where([
['shopify_shopper_id', '=', $shopperId],
['cart_complete', '=', 1],
])->get();
}
我在返回 $completedItems 时遇到问题,看起来像这样
{
"data": {
"successful": true,
"message": "All completed items",
"data": [
{
"id": 20,
"shopify_shopper_id": 19,
"shopify_shop_id": 1,
"variant_id": "3",
"qty": "1",
"cart_complete": 1,
"created_at": "2020-07-30T13:37:58.000000Z",
"updated_at": "2020-07-30T14:07:08.000000Z",
"user": {
"id": 19,
"token": "9BW31WKtW4palubBtiyw2fP8jVFsxle5",
"firstname": null,
"lastname": null,
"contact_num": null,
"add_number": null,
"add_address": null,
"country_id": null,
"email": null,
"email_verified_at": null,
"password": null,
"created_at": "2020-07-30T13:12:23.000000Z",
"updated_at": "2020-07-30T13:12:23.000000Z",
"deleted_at": null
}
},
{
"id": 21,
"shopify_shopper_id": 19,
"shopify_shop_id": 1,
"variant_id": "4",
"qty": "8",
"cart_complete": 1,
"created_at": "2020-07-30T13:38:00.000000Z",
"updated_at": "2020-07-30T14:57:00.000000Z",
"user": {
"id": 19,
"token": "9BW31WKtW4palubBtiyw2fP8jVFsxle5",
"firstname": null,
"lastname": null,
"contact_num": null,
"add_number": null,
"add_address": null,
"country_id": null,
"email": null,
"email_verified_at": null,
"password": null,
"created_at": "2020-07-30T13:12:23.000000Z",
"updated_at": "2020-07-30T13:12:23.000000Z",
"deleted_at": null
}
},
{
"id": 22,
"shopify_shopper_id": 19,
"shopify_shop_id": 1,
"variant_id": "4",
"qty": "1",
"cart_complete": 1,
"created_at": "2020-07-30T15:01:16.000000Z",
"updated_at": "2020-07-30T15:04:22.000000Z",
"user": {
"id": 19,
"token": "9BW31WKtW4palubBtiyw2fP8jVFsxle5",
"firstname": null,
"lastname": null,
"contact_num": null,
"add_number": null,
"add_address": null,
"country_id": null,
"email": null,
"email_verified_at": null,
"password": null,
"created_at": "2020-07-30T13:12:23.000000Z",
"updated_at": "2020-07-30T13:12:23.000000Z",
"deleted_at": null
}
}
]
}
}
但是我不想拥有 “数据”:[ { 我希望它像 "data": { 而不是任何建议??
【问题讨论】:
标签: arrays laravel api eloquent