-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pivot record instance ->save() updates multiple rows #21425
Comments
Okay never mind I solved it by adding update "action_ticket" set "metadata" = 'bla', "updated_at" = '---' where "id"=idOfPivotRecord |
@marijnhurkens how do you add id on withPivot functuion ? sample code please . i have same problem but and cant fix it .this is my code . where i add id ? |
Just add public function actions() {
return $this->belongsToMany(TicketAction::class)->withPivot([
'id',
optional other columns here...
])->withTimestamps()->using(TicketTicketAction::class);
} |
@marijnhurkens
then i execute this code i expect just row 1 updated and count set to 9 but multiple rows updated :
and now table : what should i do ? |
First of all you can use three backticks (`) to insert code blocks on github. This makes the code more readable. Second I assume you mean this?
Which Laravel version do you use? |
yes i mean |
Okay, I'm using 5.5, could be this issue. If you want to stay at 5.4 you can try: $post = Post:find(1);
$comment = $post->comments()->wherePivot('count','=',5)->first();
$post->comments()->updateExistingPivot($comment->id, ['count' => 9]);
// no save() needed I think Update: You can also try to dump the generated sql query or use the laravel-debugbar module: https://github.com/barryvdh/laravel-debugbar |
thanks its woked but i think this code consumer more query one for getting comment->id and another for update ,if you find better solution please post here,thank you for help ! @marijnhurkens |
Description:
I have 2 models:
Tickets
andActions
which have a n:n relationship. The pivot model (ActionTicket
) has a field callorder
because a ticket can have multiple actions of the same type.The pivot model also has some other metadata. Now at some point I want to update the metadata on the pivot model. This is my code:
With the database rows from the example above I have the following problem. When I execute the code with
$order = 3
then the metadata field on rows 1 and 3 are updated. The query generated is as follows:As you can see the order is gone.
How can I solve this problem?
The text was updated successfully, but these errors were encountered: