在Laravel中具有多个forieng键的BELONGOMANY

在Laravel中具有多个forieng键的BELONGOMANY,laravel,Laravel,默认情况下,laravel在透视表中支持两个foreing_键。“附着”和“分离”方法都能正常工作。但是,如果数据透视表中有三个外键,那么检索数据并将数据插入数据透视表的正确方法是什么 表格 Orders: id bill_number Products id name Colors id name order_details: id order_id product_id color_id price

默认情况下,laravel在透视表中支持两个foreing_键。“附着”和“分离”方法都能正常工作。但是,如果数据透视表中有三个外键,那么检索数据并将数据插入数据透视表的正确方法是什么

表格

Orders:
id       bill_number

Products
id       name

Colors
id       name

order_details:
id       order_id       product_id        color_id        price
我的型号:

class Order extends Model
{

    public function client() {
        return $this->belongsTo('App\Client');
    }
    public function items() {
        return $this->belongsToMany('App\Product', 'order_items');
    }
}

class Product extends Model
{
    //
    public function orders() {
        return $this->belongsToMany('App\Order', 'order_items');
    }
}

class Color extends Model
{
    //
    public function orders() {
        return $this->belongsToMany('App\Order', 'order_items');
    }
}
现在,我想获取或插入具有以下属性的order_项目:

order_id       product_id       color_id       price
1              1                1              800
产品:

class Order extends Model
{

    public function client() {
        return $this->belongsTo('App\Client');
    }
    public function items() {
        return $this->belongsToMany('App\Product', 'order_items');
    }
}

class Product extends Model
{
    //
    public function orders() {
        return $this->belongsToMany('App\Order', 'order_items');
    }
}

class Color extends Model
{
    //
    public function orders() {
        return $this->belongsToMany('App\Order', 'order_items');
    }
}
  • 衬衫
  • 喘息
  • 外套
  • 颜色:

    class Order extends Model
    {
    
        public function client() {
            return $this->belongsTo('App\Client');
        }
        public function items() {
            return $this->belongsToMany('App\Product', 'order_items');
        }
    }
    
    class Product extends Model
    {
        //
        public function orders() {
            return $this->belongsToMany('App\Order', 'order_items');
        }
    }
    
    class Color extends Model
    {
        //
        public function orders() {
            return $this->belongsToMany('App\Order', 'order_items');
        }
    }
    
  • 灰色的
  • 白色的
  • 黑色的
  • 现在,我想列出所有项目的订单与产品名称,颜色和价格;
    如果我想订购一件价格为800美元的黑色衬衫,我该如何计算?此外,如果我列出所有带有颜色和价格的订单项目,我应该使用哪种方法?

    我将使用双透视表,使用相同的订单id来显示订单产品和订单颜色。另一种解决方案是使用多态关系,您可以使用。然后您可以在该模型上定义与颜色的关系。

    有点“黑”,但您可以使用
    with pivot
    where pivot

    class Product extends Model
    {
        public function orders() {
            return $this->belongsToMany('App\Order', 'order_items')->withPivot('color_id', 'price');
        }
    }
    
    然后,您可以像这样插入新记录:

    $product->orders()->attach($orderId, ['color_id' => 1, 'price' => 800]);
    
    并通过以下方式查询:

    $products = Product::whereHas('orders', function ($query) {
        $query->where('color_id', '=', 1);
    })->get();
    

    我终于用sql join实现了以下功能:

    class Order extends Model
    {
        public function items() {
    
            return $this->belongsToMany('App\Product', 'order_details')->withPivot('id', 'price')
                            ->join('colors', 'order_details.color_id', '=', 'colors.id')
                            ->select('products.*', 'colors.name as color_name');
        }
    }
    

    谢谢你@jason houle。这是一个很好的选择,但我不知道如何使用产品名称和颜色名称查询项目?我没有测试过这一点,但我认为下面的方法可以工作:
    $order->with(['product'=>function($query){$query->where('id',$productID);},'color'=>function($query){$query->where('id',$colorID)}->get()谢谢@Luisjorkera你能帮我查询两个数据透视表吗?或者如果你能帮助我如何设计多态关系?谢谢。我照你说的做了。但当我列出我的订单项目时,我可以得到产品名称,但我也不能得到颜色名称。你能帮我吗?@AliBabaAzimi当你在迭代你的订单时
    foreach($product->orders as$order)
    你可以访问
    pivot
    属性。所以你可能会得到这样的结果:
    $order->pivot->color\u id可以,但我不需要id,我需要颜色名称