Wizix's avatar
Level 1

UpdateExistingPivot method doesn't work

Hey guys,

I'm trying to update a value in a pivot table. This is my method :

public function updateStatus(Event $event)
{
    $this->authorize('updateStatus', $event);

    $newStatus = Input::get('status');
    $actualPivot = $event->guests()->where('user_id', Auth::id())->first()->pivot;

    $id = $actualPivot['id'];
    $status = $actualPivot['status'];

    if ($newStatus != $status)
    {
        dd($event->guests()->updateExistingPivot($id, ['status' => $newStatus]));
    }

    return back();
}

I've checked with HeidiSQL, the row isn't updated how it should be. I've also tried this solution, but it doesn't update the row, it creates a new one. There is the dd() with this method:

array:3 [▼
  "attached" => array:1 [▼
    0 => 1
  ]
  "detached" => []
  "updated" => []
]

This is my guests() relation defined in the Event model:

public function guests()
{
    return $this->belongsToMany('App\User')
                ->using('App\Invitation')
                ->withPivot('id', 'status')
                ->withTimestamps();
}

I don't know why the updateExistingPivot() method doesn't work. I hope you can help.

Thanks!

0 likes
2 replies
kamleshpant's avatar
Level 1

while using updateExistingPivot($userId, $columnArray) you need to give first parameter as $userId not $eventId. this will solve your problem

2 likes

Please or to participate in this conversation.