0

I am facing issue for creating assosications based on multiple columns. Here is my code and array :-

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

And My OrdersProduct.php Model file

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

See the array below:-

  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
                    )

            )

    )
)

My expected output is below:-

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
                    )

            )

    )
)

I want to compare with product_id and product_size in order_products table form products_color table having product_id and size. thanks

H45H
  • 1,019
  • 10
  • 28
kunal
  • 255
  • 3
  • 17

3 Answers3

0

Compoships adds support for multi-columns relationships in Laravel 5's Eloquent.

It allows you to specify relationships using the following syntax:

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

where columns have to match.

H45H
  • 1,019
  • 10
  • 28
  • 1
    you have copied from this url https://stackoverflow.com/questions/29751859/laravel-5-hasmany-relationship-on-two-columns/49739512#49739512 – kunal Dec 21 '18 at 06:16
  • I would like to see at what point you are comparing values. Please show. – H45H Dec 21 '18 at 06:19
  • return $this->hasMany('App\ProductsColor', ['product_id', 'product_id'], ['product_size', 'size']); – kunal Dec 21 '18 at 06:19
  • have you tried [this](https://stackoverflow.com/questions/28913014/laravel-eloquent-search-on-fields-of-related-model) as a reference. – H45H Dec 21 '18 at 06:21
0

try this in your OrdersProduct model :

public function getattributes(){
   return $this->hasMany('App\ProductsColor','product_id','product_id')->where('size',$this->product_size);
 }
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
0

Finally done. i have add the join in getattributes. Hope it will help others in future:-

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