dimanche 20 mai 2018

Laravel multiple sync in self refference pivot table

Product Model:

public function related_product()
    {
        return $this->belongsToMany(Product::class, 'related_product', 'product_id', 'related_product_id');
    }

// Same table, self-referencing, but different the key order
    public function related_products()
    {
        return $this->belongsToMany(Product::class, 'related_product', 'related_product_id', 'product_id');
    }

Now I tried to make them sync by :

$product->related_product()->sync($request->related_products);
$product->related_products()->sync($request->related_products);

It is relating only posted value with the current object. Example: Suppose A Tshirt have 4 colors. White, Black, Blue, Red. I am posting [Black, Blue, Red] from the white. My code is doing something like this:

white -> Black
White -> Blue
White -> Red

Black -> white 
Blue -> white 
Red -> white 

But I want few more.

white -> Black
White -> Blue
White -> Red

Black -> white 
Blue -> white 
Red -> white 

Black -> Blue
Black -> Red
Blue - > Black 
Blue - > Red 
Red - > Black 
Red - > Blue



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IvAxNX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire