【问题标题】:Laravel many to many need column from join tableLaravel 多对多需要来自连接表的列
【发布时间】:2018-08-18 21:50:58
【问题描述】:

我的两张表是这样关联的

public function orders() { 
    return $this->belongsToMany('App\Order'); 
}

public function products() { 
    return $this->belongsToMany('App\Product', 'OrderDetails');
}

如何获取数量列?在 order_product 表上定义

【问题讨论】:

  • 你能详细解释一下你的问题吗?
  • 你在哪里保存数量?您的数据库设置似乎有一些非常糟糕的概念。多描述一下!

标签: laravel eloquent many-to-many


【解决方案1】:

使用withPivot():

public function orders() { 
    return $this->belongsToMany('App\Order')->withPivot('quantity'); 
}

$orders = Product::find($id)->orders;
foreach($orders as $order) {
    // $order->pivot->quantity
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多