update existing pivot table data in laravel

Updated at 18-Jul-2021, By samar

update existing pivot table data in laravel

We’ll attempt to use programming in this lesson to solve the "update existing pivot table data in laravel" puzzle.

You can update existing pivot table column values in laravel using updateExistingPivot relationship method in laravel.
  • Update existing pivot table column value in laravel 8

    //Inside Controller's method 
    $user = App\Models\User::find(1);
    $roleId = 1;
    $attributes = ['order_by' => 2];
    $user->roles()->updateExistingPivot($roleId, $attributes);
    

    It will update the column value of the existing record of pivot table in laravel 8. You can pass multiple column values per your requirement in the attributes array. updateExistingPivot method will update the user which has role id 1 with pivot column order_by by value of 2.

Back to code snippet queries related laravel