I am attaching user_id, product_id with an extra field. Every thing is working fine until the extra field should be updated. When the field will be filled for second time instead of updating it will add another one to database. and it's obvious because I used attach instead of sync. But when I use sync I get an error.
this is my code:
$price = $request->input('price');
$product = Product::find($id);
$product->users()->attach(Auth::id(), ['price' => $price]);
and this is the error I get when I use sync:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::formatRecordsList() must be of the type array, integer given
The first parameter of the sync()
method should be an array. So correct syntax is:
$product->users()->sync([Auth::id() => ['price' => $price]]);
https://laravel.com/docs/5.4/eloquent-relationships#updating-many-to-many-relationships
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With