Laravel如果父母数据不存在,我该如何存放父母的孩子

发布于 2025-01-19 18:06:40 字数 6158 浏览 2 评论 0原文

因此,我想在我的Laravel API中创建一个卡产品功能,我创建了一张桌子,其中产品属于Cartsdetail和Carts有许多CartsDetail,我在CartDetail Controller中制作了一个功能商店,我想将一些CartDetail存储在1个购物车中,但我是我的。当我将数据存储在Postman中时,在CartDetail Controller中遇到问题,购物车ID还会增加。我希望它就像数据推车(CARTS_ID)不存在 - 商店request-> carts_id,如果存在返回存储购物车details。 ERD看起来像这样

我的CartDetail控制器:

public function store(Request $request)
 $this->validate($request, [
            'carts_id' => 'required',
            'products_id' => 'required',
            'quantity' => 'required'
  ]);
        $useritem = $request->user();
        $productitem = Products::findOrFail($request->products_id);
        $cart = Cart::where('users_id', $useritem->id)->where('status', 'carts')->first();

        if ($cart) {
            $itemcart = $cart;
        }else{
            $no_invoice = Cart::where('users_id', $useritem->id)->count();
            $inputancart['id'] = $request->carts_id;
            $inputancart['users_id']= $useritem->id;
            $inputancart['no_resi'] = Str::random(10).' ' .str_pad(($no_invoice + 1), '3', '0', STR_PAD_LEFT);
            $inputancart['status'] = 'cart';
            $itemcart = Cart::create($inputancart);
        }
        $cekdetail = CartDetails::where('carts_id', $itemcart->id)->where('products_id', $productitem->id)->first();
        $quantity = $request->quantity;
        $price = $productitem->price;
        $discount = $productitem->discount != null ? $productitem->discount->discount_nominal: 0;
        $subtotal = ($quantity * $price) - $discount;

        if ($cekdetail){
            $cekdetail->updatedetail($cekdetail, $quantity, $price, $discount);
            $cekdetail->carts->updatetotal($cekdetail, $quantity, $price, $discount);
        } else {
            $inputan = $request->all();
            $inputan['carts_id'] = $request->carts_id;
            $inputan['products_id'] = $productitem->id;
            $inputan['quantity'] = $quantity;
            $inputan['price'] = $price;
            $inputan['discount'] = $discount;
            $inputan['subtotal'] = ($price * $quantity) - $discount;
            $itemdetail = CartDetails::create($inputan);
            $itemdetail->carts->updatetotal($itemdetail->carts, $subtotal);
        }
        return response()->json(['Product successfully added to cart']);
    }


//model cartdetail
public function updatedetail($itemdetail, $quantity, $price, $voucher){
    $this->attributes['quantity'] = $itemdetail->quantity + $quantity;
    $this->attributs['subtotal'] = $itemdetail->subtotal + ($quantity * ($price - $voucher));
    self::save();
}
public function updatetotal($itemcart, $subtotal){
    $this->attributes['subtotal']= $itemcart->subtotal + $subtotal;
    $this->attributes['total']= $itemcart->total +$subtotal;
    self::save();
}

//index cart controller
 public function index(Request $request)
{
    $itemuser = $request->user();
    $itemcart = Cart::where('users_id', $itemuser->id)->where('status', 'cart')->with('cartdetails')->get();
    return response()->json(['data' => 'Shopping cart', 'itemcart'=> $itemcart], 300);
}

我认为问题仅在这里

(编辑)这不是可见的错误,但是它复制了我的父级数据(购物车),第一店不是

first store was :   
"itemcart": [
        {
            "id": 1,
            "users_id": 3,
            "no_resi": "oxIkpZ3xhl 001",
            "discount": 0,
            "subtotal": 300000,
            "status": "cart",
            "total": 300000,
            "created_at": "2022-04-08T06:25:06.000000Z",
            "updated_at": "2022-04-08T06:25:06.000000Z",
            "cartdetails": [
                {
                    "id": 1,
                    "products_id": 2,
                    "carts_id": 1,
                    "quantity": 3,
                    "discount": 0,
                    "price": 100000,
                    "subtotal": 300000,
                    "created_at": "2022-04-08T06:25:06.000000Z",
                    "updated_at": "2022-04-08T06:25:06.000000Z"
                }
            ]
        }

and then to this :
 "data": "Shopping cart",
    "itemcart": [
        {
            "id": 1,
            "users_id": 3,
            "no_resi": "oxIkpZ3xhl 001",
            "discount": 0,
            "subtotal": 599091,
            "status": "cart",
            "total": 599091,
            "created_at": "2022-04-08T06:25:06.000000Z",
            "updated_at": "2022-04-08T06:29:22.000000Z",
            "cartdetails": [
                {
                    "id": 1,
                    "products_id": 2,
                    "carts_id": 1,
                    "quantity": 3,
                    "discount": 0,
                    "price": 100000,
                    "subtotal": 300000,
                    "created_at": "2022-04-08T06:25:06.000000Z",
                    "updated_at": "2022-04-08T06:25:06.000000Z"
                },
                {
                    "id": 2,
                    "products_id": 4,
                    "carts_id": 1,
                    "quantity": 3,
                    "discount": 0,
                    "price": 99697,
                    "subtotal": 299091,
                    "created_at": "2022-04-08T06:29:22.000000Z",
                    "updated_at": "2022-04-08T06:29:22.000000Z"
                }
            ]
        },
        {
            "id": 2,
            "users_id": 3,
            "no_resi": "Nf0BkdaxTk 002",
            "discount": 0,
            "subtotal": 0,
            "status": "cart",
            "total": 0,
            "created_at": "2022-04-08T06:29:22.000000Z",
            "updated_at": "2022-04-08T06:29:22.000000Z",
            "cartdetails": []
        }
    ]

问题相同的价值商店,但父母

非常感谢!对于任何人都可以解决我的问题。

so i want to create a card product feature in my laravel api, i create a table where products belongs to cartsdetail and carts has many cartsdetail, i make a function store in cartdetail controller that i want to store some cartdetail in 1 carts ,but i have problem in cartdetail controller when I store data in postman , there is also an addition to the carts id. i want it to be like if data carts(carts_id) doesnt exist->store request->carts_id, if exist return store cartsdetails.
the ERD looks like this
the ERD looks like this

my cartdetail controller :

public function store(Request $request)
 $this->validate($request, [
            'carts_id' => 'required',
            'products_id' => 'required',
            'quantity' => 'required'
  ]);
        $useritem = $request->user();
        $productitem = Products::findOrFail($request->products_id);
        $cart = Cart::where('users_id', $useritem->id)->where('status', 'carts')->first();

        if ($cart) {
            $itemcart = $cart;
        }else{
            $no_invoice = Cart::where('users_id', $useritem->id)->count();
            $inputancart['id'] = $request->carts_id;
            $inputancart['users_id']= $useritem->id;
            $inputancart['no_resi'] = Str::random(10).' ' .str_pad(($no_invoice + 1), '3', '0', STR_PAD_LEFT);
            $inputancart['status'] = 'cart';
            $itemcart = Cart::create($inputancart);
        }
        $cekdetail = CartDetails::where('carts_id', $itemcart->id)->where('products_id', $productitem->id)->first();
        $quantity = $request->quantity;
        $price = $productitem->price;
        $discount = $productitem->discount != null ? $productitem->discount->discount_nominal: 0;
        $subtotal = ($quantity * $price) - $discount;

        if ($cekdetail){
            $cekdetail->updatedetail($cekdetail, $quantity, $price, $discount);
            $cekdetail->carts->updatetotal($cekdetail, $quantity, $price, $discount);
        } else {
            $inputan = $request->all();
            $inputan['carts_id'] = $request->carts_id;
            $inputan['products_id'] = $productitem->id;
            $inputan['quantity'] = $quantity;
            $inputan['price'] = $price;
            $inputan['discount'] = $discount;
            $inputan['subtotal'] = ($price * $quantity) - $discount;
            $itemdetail = CartDetails::create($inputan);
            $itemdetail->carts->updatetotal($itemdetail->carts, $subtotal);
        }
        return response()->json(['Product successfully added to cart']);
    }


//model cartdetail
public function updatedetail($itemdetail, $quantity, $price, $voucher){
    $this->attributes['quantity'] = $itemdetail->quantity + $quantity;
    $this->attributs['subtotal'] = $itemdetail->subtotal + ($quantity * ($price - $voucher));
    self::save();
}
public function updatetotal($itemcart, $subtotal){
    $this->attributes['subtotal']= $itemcart->subtotal + $subtotal;
    $this->attributes['total']= $itemcart->total +$subtotal;
    self::save();
}

//index cart controller
 public function index(Request $request)
{
    $itemuser = $request->user();
    $itemcart = Cart::where('users_id', $itemuser->id)->where('status', 'cart')->with('cartdetails')->get();
    return response()->json(['data' => 'Shopping cart', 'itemcart'=> $itemcart], 300);
}

I think the problem is only here
I think the problem is only here

(edit) this is not error visible, but it duplicate my parent data(carts), first store is not problem but the second time to more that duplicate my parent data like this

first store was :   
"itemcart": [
        {
            "id": 1,
            "users_id": 3,
            "no_resi": "oxIkpZ3xhl 001",
            "discount": 0,
            "subtotal": 300000,
            "status": "cart",
            "total": 300000,
            "created_at": "2022-04-08T06:25:06.000000Z",
            "updated_at": "2022-04-08T06:25:06.000000Z",
            "cartdetails": [
                {
                    "id": 1,
                    "products_id": 2,
                    "carts_id": 1,
                    "quantity": 3,
                    "discount": 0,
                    "price": 100000,
                    "subtotal": 300000,
                    "created_at": "2022-04-08T06:25:06.000000Z",
                    "updated_at": "2022-04-08T06:25:06.000000Z"
                }
            ]
        }

and then to this :
 "data": "Shopping cart",
    "itemcart": [
        {
            "id": 1,
            "users_id": 3,
            "no_resi": "oxIkpZ3xhl 001",
            "discount": 0,
            "subtotal": 599091,
            "status": "cart",
            "total": 599091,
            "created_at": "2022-04-08T06:25:06.000000Z",
            "updated_at": "2022-04-08T06:29:22.000000Z",
            "cartdetails": [
                {
                    "id": 1,
                    "products_id": 2,
                    "carts_id": 1,
                    "quantity": 3,
                    "discount": 0,
                    "price": 100000,
                    "subtotal": 300000,
                    "created_at": "2022-04-08T06:25:06.000000Z",
                    "updated_at": "2022-04-08T06:25:06.000000Z"
                },
                {
                    "id": 2,
                    "products_id": 4,
                    "carts_id": 1,
                    "quantity": 3,
                    "discount": 0,
                    "price": 99697,
                    "subtotal": 299091,
                    "created_at": "2022-04-08T06:29:22.000000Z",
                    "updated_at": "2022-04-08T06:29:22.000000Z"
                }
            ]
        },
        {
            "id": 2,
            "users_id": 3,
            "no_resi": "Nf0BkdaxTk 002",
            "discount": 0,
            "subtotal": 0,
            "status": "cart",
            "total": 0,
            "created_at": "2022-04-08T06:29:22.000000Z",
            "updated_at": "2022-04-08T06:29:22.000000Z",
            "cartdetails": []
        }
    ]

with the same value store but the parent become duplicate

big thanks! for anyone can solve my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文