For getting every new tutorial link Please join our telegram group

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

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

Laravel mass update and save method

  1. save()
  2. update()

Laravel save() method

Laravel save method use to update a model data firstly we get a model data and than change something in its column and after apply save() method for updating data. Let see example 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 = 'anmol sharma';
    $student->age = '24';
    $student->save();
  }

}

Laravel update and massupdate data

laravel uses the update method to update a single or multiple row data. First, we get a model row by where condition then applies update method. In the update method, we pass an array of columns with values.

update() method used to update all data that are marching in where condition so it also called mass assignment. When we use the update method then also give 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']);
  }

}

You read this tutorial on advanced web tutorial. here we provide laravel beginners to advanced laravel tutorial.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links