【问题标题】:Laravel Return IssueLaravel 退货问题
【发布时间】: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


    【解决方案1】:

    在您的getCompleteItems 函数中,您尝试get() 所有已完成的项目,这将返回"data": [ {,因为它是一个对象数组,但如果您想要"data": {,则必须执行first(),这将返回第一个完成的项目,在做之前仔细检查你的逻辑first()

    【讨论】:

    • 那只会返回我可能有更多的第一个条目
    • 确实,问题是什么将完成的项目分组?必须有一个东西数组,数组数组或对象数组。
    • 我只是想返回对象 - 我已经用不止一个对象更新了问题
    • 好的,让我们分解一下 json 格式,看起来像 {"key": "value"} 您的密钥是 data 并且您的值必须是单个值,它可以是 stringintegerarray您想以value 的形式返回许多对象,例如{"key": "value1", "value2"},这不是有效的json 格式value1, value2 必须分组为array,例如{"key": ["value1", "value2"]}
    猜你喜欢
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    相关资源
    最近更新 更多