Skip to content
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

Closed
marijnhurkens opened this issue Sep 27, 2017 · 8 comments
Closed

Pivot record instance ->save() updates multiple rows #21425

marijnhurkens opened this issue Sep 27, 2017 · 8 comments

Comments

@marijnhurkens
Copy link

  • Laravel Version: 5.5.13
  • PHP Version: 7.1
  • Database Driver: SQlite

Description:

I have 2 models: Tickets and Actions which have a n:n relationship. The pivot model (ActionTicket) has a field call order because a ticket can have multiple actions of the same type.

Ticket 1, action 1, order 1
Ticket 1, action 2, order 2,
Ticket 1, action 1, order 3
etc.

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:

$pivotmodel = $ticket->actions()->wherePivot('order', '=', $order)->first()->pivot;
$pivotmodel->metadata = 'bla';
$pivotmodel->save();

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:

update "action_ticket" set "metadata" = 'bla', "updated_at" = '---' where "ticket_id" = '1' and "ticket_action_id" = '1'

As you can see the order is gone.

How can I solve this problem?

@marijnhurkens marijnhurkens changed the title Pivot record instance save() updates multiple rows Pivot record instance ->save() updates multiple rows Sep 27, 2017
@marijnhurkens
Copy link
Author

marijnhurkens commented Sep 27, 2017

Okay never mind I solved it by adding id to the withPivot() function and now the update query is:

update "action_ticket" set "metadata" = 'bla', "updated_at" = '---' where "id"=idOfPivotRecord

@mahdiahmadi73
Copy link

@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 ?
public function post()
{
return $this->belongsToMany('App\Post','comment_post','comment_id','post_id')
->withPivot('count');
}

@marijnhurkens
Copy link
Author

marijnhurkens commented Oct 24, 2017

Just add id to the array you can pass to withPivot. Example:

public function actions() {
        return $this->belongsToMany(TicketAction::class)->withPivot([
            'id',
            optional other columns here...
        ])->withTimestamps()->using(TicketTicketAction::class);
    }

@mahdiahmadi73
Copy link

mahdiahmadi73 commented Oct 24, 2017

@marijnhurkens
not working
i have two model post and comment with manyToMany relation .pivot table is comment_post and pivot column is count , i changed my code to ;

public function comments()
{
return $this->belongsToMany('App\Comment','comment_post','post_id','comment_id')
->withPivot(['id','count']);
}

here is my table :
1

then i execute this code i expect just row 1 updated and count set to 9 but multiple rows updated :

$post=Post:find(1);
$comment=$post->comments()->wherePivot('count','=',5)->first()->pivot ;
$comment->count=9 ;
$comment->save() ;

and now table :

2

what should i do ?

@marijnhurkens
Copy link
Author

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?

$comment->count = 9 ;

Which Laravel version do you use?

@mahdiahmadi73
Copy link

yes i mean $comment->count=9 ;
i using laravel 5.4

@marijnhurkens
Copy link
Author

marijnhurkens commented Oct 24, 2017

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

@mahdiahmadi73
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants