Site Admin
Site Admin Founder of MeaningArticles
3324 Views

laravel eloquent orm model data update by save() and update() method

Hello Dev.

In this laravel articles we will discuss how to update eloquent model data by update() and save() method with example.

Laravel mass update and save method

In laravel we are able to show you the way to update eloquent model records through update() and save() method in details.
save()
update()


Laravel save() method

Laravel save method use to update a model data first of all we get a model data and than change something in its column and after apply save() method for updating records. allow see instance of save method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller{
 public function index(){
    $id = 7;
    $student = Student::find($id);
    $student->name = 'john doe';
    $student->age = '23';
    $student->save();
  }

}


Laravel update and massupdate data

laravel makes use of the update technique to replace a single or multiple row records. First, we get a model row via where condition then applies update method. in the update method, we pass an array of columns with values.

update() method used to update all records which might be marching in where condition so it also called mass assignment. while we use the update technique then additionally provide some configuration in the model like protected $guarded = [];.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller{
 public function index(){
    $student = Student::where('salary', '>' ,'10000')->update(['position', 'senior']);
  }

}

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.