首页 > 解决方案 > Laravel:如何在 Crinsane 购物车上传递其他参数

问题描述

我必须purchase_prices将表格中的字段product传递到 crinsane 购物车,以便我可以查看profit每笔交易的内容并将其保存到数据库中。

这是我在CartController上的代码

$product = Product::find($id);

Cart::add([
    'id' => $product->id,
    'name' => $product->name,
    'qty' => 1,
    'price' => $product->sell_price,
    'options' => ['purchase' => $product->purchase_price]
]);

return back();

但购买未存储

Collection {#257 ▼
  #items: array:1 [▼
    "370d08585360f5c568b18d1f2e4ca1df" => CartItem {#258 ▼
      +rowId: "370d08585360f5c568b18d1f2e4ca1df"
      +id: 2
      +qty: 1
      +name: "Beras B"
      +price: 12000.0
      +weight: 1.0
      +options: CartItemOptions {#259 ▼
        #items: []
      }
      -associatedModel: null
      -taxRate: 0
      -discountRate: 0
    }
  ]
}

标签: laravelshopping-cartlaravel-5.8

解决方案


@foreach ($cartItems as $cartItem)
    <?php $no++ ?>
    <tr>
        <td>{{$no}}</td>
        <td>{{$cartItem->name}}</td>
        <td>{{$cartItem->qty}}</td>
        <td>{{$cartItem->price}}</td>
        <td>{{$cartItem->options->purchase}}</td>
        <td></td>
        <td>
          <a href="" class="btn btn-success btn-sm">Update</a>
          <a href="" class="btn btn-danger btn-sm">Delete</a>
        </td>
    </tr>
@endforeach

我看到您在编写时试图从表中检索多行

{{$cartItem->选项->购买}}

您应该可以将其设置为要显示的特定行名,并且它应该与表中使用的名称相匹配。


推荐阅读