如何计算订单总价?

如何计算订单总价?,第1张

如何计算订单总价?

您可以通过访问属性来检索 _数据透视_表的列,该

pivot
属性已经存在了很长时间了

默认情况下,只有模型关键点会出现在枢轴对象上。如果数据透视表包含额外的属性,则在定义关系时必须指定它们:

在您的情况下,您可以像下面的代码中那样定义关系:

class Order extends Model {    public function products()    {        return $this->belongsToMany(Order::class)->withPivot('quantity')    }}

现在,可以通过属性(例如)访问表

quantity
上的列。
order_product``pivot``$order->products()->first()->pivot->quantity

最后,这是计算订单总数的结果代码:

$total = 0;foreach ($product as $products) {    $total += $product->price * $product->pivot->quantity}$total += $order->address()->city->shipping_charges


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5011176.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存