laravel - Updating a pivot table -
i have pivot table
id | product_id | user_id
i wish update pivot table, by:
product::find($productid)->attach($userid);
this produce like
id | product_id | user_id 1 1 1
but when again not want update pivot table if values exists.
the above attach method like:
id | product_id | user_id 1 1 1 1 1 1
i know can use sync, removes table, not wish this. know can use:
product::find(1)->user()->updateexistingpivot(1, []);
but works when data in table.
what im looking way firstorcreate on pivot table.
you can add boolean sync method adds value , doesn't remove existing value.
the code looks this. first value can int or array.
product::find(1)->user()->sync([1], false);
Comments
Post a Comment