Laravel synchronizes many errors

I add user_id, product_id with an extra field. Everything works fine until the extra field is updated. When the field is filled a second time instead of updating, it will add another one to the database. and this is obvious because I used binding instead of synchronization. But when I use synchronization, I get an error message.

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 using synchronization:

Argument 1 went to Lighten \ Database \ Eloquent \ Relations \ BelongsToMany :: formatRecordsList () must be an array of types, integer given

+4
source share
3 answers

sync() . :

$product->users()->sync([Auth::id() => ['price' => $price]]);

https://laravel.com/docs/5.4/eloquent-relationships#updating-many-to-many-relationships

+5

, .

,

$product->users()->sync([Auth::id() => ['price' => $price]]);
+2

. , , . , sync()

$product->users()->sync([Auth::id() => ['price' => $price]]);
+1

Source: https://habr.com/ru/post/1679592/


All Articles