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
- save()
- 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.
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
- What is laravel eloquent model and introduction
- part 1 laravel eloquent model all configuration hindi
- part 2 laravel insert and create data by eloquent model tutorial
- part 3 laravel data retrieve method
- part 4 first, find, findOrFail and firstOrFail method
- part 5 laravel delete vs truncate method
- part 6 laravel save vs update method
- part 7 laravel updateOrCreate and upsert
- part 8 laravel chunk vs cursor method
- part 9 laravel eloquent subquery select
- part 10 laravel findOrFail vs firstOrFail
- part 11 laravel create and retrieve data
- part 12 laravel eloquent aggregate functions
- part 13 laravel isDirty vs isClean vs wasChanged method