Update Records in Database using Laravel

In This Chapter You Will Learn how To update Records in Laravel. syntax for update a record in laravel

Syntaxint update(string $query, array $bindings = array())
  • $query(string) − query to execute in database
  • $bindings(array) − values to bind with queries
DescriptionRun an update statement against the database.

We Will Explain Step by Step to Update Records in Laravel

Step 1 − Execute the below command to create a controller called StudViewController.

php artisan make:controller StudUpdateController --plain

Step 2 − After successful execution, you will receive the following output Like−

Step 3 − Copy the code to file app/Http/Controllers/ StudUpdateController.php

   public function show($id) {
      $users = DB::select('select * from student where id = ?',[$id]);
      return view('stud_update',['users'=>$users]);
   public function edit(Request $request,$id) {
      $name = $request->input('stud_name');
      DB::update('update student set name = ? where id = ?',[$name,$id]);
      echo "Record updated successfully.
"; echo 'Click Here to go back.'; } }

Step 4 − Create a view file called resources/views/stud_edit_view.blade.php and copy the following code in that file.

      View Student Records
         @foreach ($users as $user)
ID Name Edit
{{ $user->id }} {{ $user->name }} Edit

Step 5 − Create another view file called resources/views/stud_update.php and copy the following code in that file.

      Student Management | Edit

Step 6 − Add the following lines in app/Http/routes.php.

Route::get('edit-records','[email protected]');
Route::get('edit/{id}','[email protected]');
Route::post('edit/{id}','[email protected]');

Step 7 − Visit the following URL to update records in database.


Step 8 − The output will appear as shown in the following imag

Step 9 − Click the edit link on any record and you will be redirected to a page where you can edit that particular record.

Step 10 − The output will appear as shown in the following image.

Step 11 − After editing that record, you will see a prompt as shown in the following image.