将数据存储在 laravel 5.8 中的 foreach 循环中

Store data in foreach loop in laravel 5.8

我在存储数据方面需要你的帮助。

我有一个包含多个产品的购物车,现在我想通过单击提交按钮

同时将所有这些产品存储在 Order table 中

我的观点blade.php

此视图显示了购物车中的所有产品,我需要将这些信息存储在 Ordertable

<form action="{{route('order.store')}}" method="post">
  @csrf
  @foreach ($cart as $ct)

  <input type="text" name="product_id" value="{{$ct->id}}" hidden>

  <input type="text" value="{{$ct->color}}" name="color" >

  <input type="text" value="{{$ct->size}}" name="size" >

  <input type="text" value="{{$ct->price}}" name="price" >

  @endforeach
 <button type="submit">submit</button>            
</form>

在我的 Order table 中,我有这 4 列需要填写:product_id、颜色、尺寸和价格。

我的 foreach 循环从 Cart table 获取数据并且所有数据都显示无误。

我的问题是如何通过单击一次提交按钮将这些数据存储到我的 Order table?我应该在 store function 中写什么 OrderController?

如果我的购物车有 3 件产品,那么我的预期值为 Order table 将如下所示:

id----product_id----size----color---price---+
---------------------------------------------
1      1             abc     xyz     123
---------------------------------------------
2      2             abc2    xyz2    456
---------------------------------------------
3      3             aaa     bbb     789

感谢您的帮助!

数据库:

Order:
    user_id
    created_at
    ...


orderProducts:
    price
    color
    size
    order_id (relation)
    product_id (relation)
    ...

查看

<form action="{{route('order.store')}}" method="post">
  @csrf
  @foreach ($cart as $ct)

  <input type="text" name="products[]" value="{{$ct->product_id}}" hidden>

  <input type="text" value="{{$ct->color}}" name="color[]"  >

  <input type="text" value="{{$ct->size}}" name="size[]" >

  <input type="text" value="{{$ct->price}}" name="price[]" >

  @endforeach
 <button type="submit">submit</button>            
</form>

控制器函数存储

        $order= new Order;// change the model here
        // .. complete your information's like user id or created_at 
        $order->save();

        foreach($request->products as $key=>$product_id)
        {
            $product = Product::findOrFail($product_id); // validations the product id 

            // get your information's from db (color,size,price) don't trust get information's from user .
            // $product->price , $product->color .... or get from cart 
           //from cart direct $color[$key] or $price[$key] or $size[$key]  "I don't recomend to use this point"


            // must be create new table
            $orderProducts=new orderProducts; // create new table ordersProducts

            $orderProducts->price = $product->price;
            // .. complete your information's

            $orderProducts->order_id = $order->id; // primary key
            $orderProducts->product_id= $product->id;

            $orderProducts->save();

        }

注意事项: - 您需要使用 try-catch 记录信息,如果在步骤 "findOrFail" 中失败或更改为查找,然后如果在 table 中找不到产品,则记录订单未完成并显示错误用户

我的答案与上面的答案非常相似,但我想在这里使用数据库事务的不同方法,尝试 Catch 和批量插入(以防止在循环中放置查询)

try {
       DB::begintransaction();
       $order = Order::create($orderData); // change the model here


   $orderProductsData = [];
   foreach($request->products as $product_id) {
        $product = Product::find($product_id); // validations the product id
        $orderProductsData[] = $tmpData = [
            'price' => $product->price,
            'order_id' => $order->id,
            'product_id' => $product->id;
        ];
        // must be create new table

        // You can also store this way, but its still in loop so putting under comment.
        // $orderProducts = orderProducts::create($tmpData); 

    }

    orderProducts::insert($orderProductsData);

  DB::commit();
} catch (Exception $e) {
   DB::rollback();
   thow $e; // modify with, How you handle your error response.
}

同样,上面的答案是完全正确的,但是只是采用了一种不同的方法,我认为这种方法没有什么更优化的方法。