mercredi 28 avril 2021

Laravel Eloquent Model can't sync with pivot data in Many-to-Many relationship

I would like to sync the data instead of attach the data to the particular relationship.

Pivot relation UserModel code

public function carts(){
        return $this->belongsToMany(Product::class,'user_carts')->withPivot('quantity');
    }

The attach code is

User::find(1)->carts()->attach($s,["quantity"=>1]);

The sync code is

User::find(1)->carts()->sync($s,["quantity"=>1]);

When I try to compile the sync, those pivot relation that matched user_id = 1 does not have the "1" in its respective quantity column.

If I would like to achieve the sync function without using attach, how can I do it because the attach() will create multiple redundant data in my database.



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

Aucun commentaire:

Enregistrer un commentaire