Bảng trung gian (ok)
https://viblo.asia/p/tim-hieu-eloquent-trong-laravel-phan-2-relationship-RnB5pym7KPG
1.3.3 Customizing thuộc tính pivot
public function products()
{
return $this->belongsToMany(Product::class)->as('detail')->withTimestamps();
}$order = Order::findOrFail(1);
foreach ($order->products as $item) {
// Truy cập vào các trường trong bảng trung gian
echo $item->detail->product_id;
}1.3.4 Filter quan hệ thông qua bảng trung gian
public function products()
{
return $this->belongsToMany(Product::class)->wherePivot('amount', '>', 3);
}
// hoặc
public function products()
{
return $this->belongsToMany(Product::class)->wherePivotIn('amount', [2,5]);
}1.3.5 Custom bảng trung gian
PreviousBảng trung gian product_order này sẽ chỉ lấy ra order_id, product_id, created_at, updated_at (ok)NextSử dụng quan hệ Many to Many dùng theo một Model mà ta tự định nghĩa (ok)
Last updated
