【问题标题】:How to make associations based on multiple columns in laravellaravel中如何根据多列进行关联
【发布时间】:2019-05-21 15:20:56
【问题描述】:

我面临基于多列创建关联的问题。这是我的代码和数组:-

$getdetails =  OrdersProduct::with('getattributes')->get();

还有我的OrdersProduct.php模型文件

public function getattributes(){
   return $this->hasMany('App\ProductsColor','product_id','product_id');
 }

请看下面的数组:-

  Array
  (
   [0] => Array
    (
        [order_id] => 100015390
        [product_id] => 1203
        [product_size] => 12
        [attributes] => Array
            (
                [0] => Array
                    (
                        [id] => 5748
                        [product_id] => 1203
                        [sku_website] => N7W84308-BLACK-10
                        [sku] => 8907613878595
                        [color] => 
                        [size] => 10
                        [price] => 2799
                        [stock] => 0
                        [ip_address] => 
                        [created_at] => 2018-08-07 16:15:36
                        [updated_at] => 2018-08-07 16:15:36
                    )

                [1] => Array
                    (
                        [id] => 5749
                        [product_id] => 1203
                        [sku_website] => N7W84308-BLACK-12
                        [sku] => 8907613878601
                        [color] => 
                        [size] => 12
                        [price] => 2799
                        [stock] => 0
                        [ip_address] => 
                        [created_at] => 2018-08-07 16:15:37
                        [updated_at] => 2018-08-07 16:15:37
                    )

            )

    )
)

我预期的输出如下:-

Array
(
[0] => Array
    (
        [order_id] => 100015390
        [product_id] => 1203
        [product_size] => 12
        [attributes] => Array
            (

                [0] => Array
                    (
                        [id] => 5749
                        [product_id] => 1203
                        [sku_website] => N7W84308-BLACK-12
                        [sku] => 8907613878601
                        [color] => 
                        [size] => 12
                        [price] => 2799
                        [stock] => 0
                        [ip_address] => 
                        [created_at] => 2018-08-07 16:15:37
                        [updated_at] => 2018-08-07 16:15:37
                    )

            )

    )
)

我想与 order_products 表中的 product_id 和 product_size 进行比较,形成 products_color 表,其中包含 product_id 和 size。谢谢

【问题讨论】:

    标签: php laravel laravel-5 eloquent query-builder


    【解决方案1】:

    Compoships 在 Laravel 5 的 Eloquent 中增加了对多列关系的支持。

    它允许您使用以下语法指定关系:

    public function b()
    {
        return $this->hasMany('B', ['key1', 'key2'], ['key1', 'key2']);
    }
    

    列必须匹配的位置。

    【讨论】:

    • 你已经从这个网址复制stackoverflow.com/questions/29751859/…
    • 我想看看你在什么时候比较值。请出示。
    • return $this->hasMany('App\ProductsColor', ['product_id', 'product_id'], ['product_size', 'size']);
    • 你试过this作为参考吗?
    【解决方案2】:

    在你的 OrdersProduct 模型中试试这个:

    public function getattributes(){
       return $this->hasMany('App\ProductsColor','product_id','product_id')->where('size',$this->product_size);
     }
    

    【讨论】:

    • 请立即回复
    【解决方案3】:

    终于搞定了。我在 getattributes 中添加了加入。希望它将来会对其他人有所帮助:-

    $getdetails =  OrdersProduct::with(['getattributes'=>function($query){
            $query->join('orders_products','orders_products.product_size','=','products_colors.size');
        }])->get();
    

    【讨论】:

    • 不,你能告诉我怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2013-08-16
    • 2018-11-24
    • 2015-12-04
    • 2019-03-28
    相关资源
    最近更新 更多