How to create and update Laravel Eloquent?

Laravel Eloquent provides powerful methods to create or update records in your database. The updateOrCreate() method and DB facade's updateOrInsert() method offer elegant solutions for upsert operations.

Prerequisites: You need Laravel installed with a configured database connection and a Student model created.

Assume we have created a table named students using the CREATE statement as shown below

CREATE TABLE students( 
   id          INTEGER       NOT NULL     PRIMARY KEY, 
   name        VARCHAR(15)   NOT NULL, 
   email       VARCHAR(20)   NOT NULL, 
   created_at  VARCHAR(27), 
   updated_at  VARCHAR(27), 
   address     VARCHAR(30)   NOT NULL, 
   age          INTEGER 
);

Assume we have populated the above table with the following data

+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ 
| id |    name       |      email       |    created_at               |          updated_at         |  address|   age| 
+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ 
| 1  | Siya Khan     | siya@gmail.com   | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz     | 20   | 
| 2  | Rehan Khan    | rehan@gmail.com  | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz     | 18   | 
| 3  | Rehan Khan    | rehan@gmail.com  | NULL                        | NULL                        | testing | 20   | 
| 4  | Rehan         | rehan@gmail.com  | NULL                        | NULL                        | abcd    | 15   | 
| 5  | Nidhi Agarwal | nidhi@gmail.com  | NULL                        | NULL                        | abcd    | 20   | 
| 6  | Ashvik Khanna | ashvik@gmail.com | NULL                        | NULL                        | oooo    | 16   | 
| 7  | Viraj Desai   | viraj@gmail.com  | NULL                        | NULL                        | test    | 18   | 
| 8  | Priya Singh   | priya@gmail.com  | NULL                        | NULL                        | test123 | 20   | 
+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+

Using updateOrCreate() Method

The updateOrCreate() method searches for records matching the first array parameter. If found, it updates the record with values from the second array. If not found, it creates a new record with combined values from both arrays.

Syntax

$model = Model::updateOrCreate(
   ['field1' => 'value'],     // Search criteria
   ['field2' => 'value2']     // Values to update/insert
);

Example 1: Creating a New Record

Let's insert a new student record using updateOrCreate()

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller {
   public function index() {
      $student = Student::updateOrCreate(
         ['name' => 'Arbaaz'],
         ['age'=> 40,'email'=>'arbaaz@gmail.com', 'address'=>'xyz']
      );
   }
}

Since there is no match for the name: Arbaaz in the student table, a new record is inserted.

+----+---------------+------------------+---------------------+---------------------+---------+------+ 
| id |    name       |        email     |     created_at      |     updated_at      | address | age  | 
+----+---------------+------------------+---------------------+---------------------+---------+------+ 
| 1  | Siya Khan     | siya@gmail.com   | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | xyz     | 20   | 
| 2  | Rehan Khan    | rehan@gmail.com  | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | xyz     | 18   |
| 3  | Rehan Khan    | rehan@gmail.com  | NULL                | NULL                | testing | 20   | 
| 4  | Rehan         | rehan@gmail.com  | NULL                | NULL                | abcd    | 15   | 
| 5  | Nidhi Agarwal | nidhi@gmail.com  | NULL                | NULL                | abcd    | 20   | 
| 6  | Ashvik Khanna | ashvik@gmail.com | NULL                | NULL                | oooo    | 16   | 
| 7  | Viraj Desai   | viraj@gmail.com  | NULL                | NULL                | test    | 18   | 
| 8  | Priya Singh   | priya@gmail.com  | NULL                | NULL                | test123 | 20   | 
| 9  | Arbaaz        | arbaaz@gmail.com | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | xyz     | 40   | 
+----+---------------+------------------+---------------------+---------------------+---------+------+

Example 2: Updating an Existing Record

Now let's update an existing record using the same method

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller {
   public function index() {
      $student = Student::updateOrCreate(
         ['name' => 'Rehan'],
         ['age'=> 50]
      );
   }
}

This example updates the age to 50 for the first record with name 'Rehan'.

+----+---------------+------------------+---------------------+---------------------+---------+------+
| id | name          |      email       |         created_at  |    updated_at       | address |  age |
+----+---------------+------------------+---------------------+---------------------+---------+------+
| 1  | Siya Khan     | siya@gmail.com   | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | xyz     | 20   |
| 2  | Rehan Khan    | rehan@gmail.com  | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | xyz     | 18   |
| 3  | Rehan Khan    | rehan@gmail.com  | NULL                | NULL                | testing | 20   |
| 4  | Rehan         | rehan@gmail.com  | NULL                | 2022-05-29 14:17:02 | abcd    | 50   |
| 5  | Nidhi Agarwal | nidhi@gmail.com  | NULL                | NULL                | abcd    | 20   |
| 6  | Ashvik Khanna | ashvik@gmail.com | NULL                | NULL                | oooo    | 16   |
| 7  | Viraj Desai   | viraj@gmail.com  | NULL                | NULL                | test    | 18   |
| 8  | Priya Singh   | priya@gmail.com  | NULL                | NULL                | test123 | 20   |
| 9  | Arbaaz        | arbaaz@gmail.com | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | xyz     | 40   |
+----+---------------+------------------+---------------------+---------------------+---------+------+

Using DB Facade updateOrInsert() Method

The DB facade provides updateOrInsert() method for similar functionality using Laravel's query builder. You need to import Illuminate\Support\Facades\DB to use this approach.

Example 3: Updating with DB Facade

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller {
   public function index() {
      DB::table('students')->updateOrInsert(
         ['age'=>40],
         ['name'=>'Arbaaz Khanna', 'email'=>'arbaaz@gmail.com', 'address'=>'testing', 'age'=>'35']
      );
   }
}

This searches for a record with age 40 and updates it with the new values.

+----+---------------+------------------+---------------------+---------------------+---------+------+
| id |      name     |       email      |      created_at     |      updated_at     | address | age  |
+----+---------------+------------------+---------------------+---------------------+---------+------+
| 1  | Siya Khan     | siya@gmail.com   | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | xyz     | 20   |
| 2  | Rehan Khan    | rehan@gmail.com  | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | xyz     | 18   |
| 3  | Rehan Khan    | rehan@gmail.com  | NULL                | NULL                | testing | 20   |
| 4  | Rehan         | rehan@gmail.com  | NULL                | 2022-05-29 14:17:02 | abcd    | 50   |
| 5  | Nidhi Agarwal | nidhi@gmail.com  | NULL                | NULL                | abcd    | 20   |
| 6  | Ashvik Khanna | ashvik@gmail.com | NULL                | NULL                | oooo    | 16   |
| 7  | Viraj Desai   | viraj@gmail.com  | NULL                | NULL                | test    | 18   |
| 8  | Priya Singh   | priya@gmail.com  | NULL                | NULL                | test123 | 20   |
| 9  | Arbaaz Khanna | arbaaz@gmail.com | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | testing | 35   |
+----+---------------+------------------+---------------------+---------------------+---------+------+

Example 4: Inserting with DB Facade

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller {
   public function index() {
      DB::table('students')->updateOrInsert(
         ['name'=>'Niketan Vaahi'],
         ['name'=>'Niketan Vaahi',
         'email'=>'niketan@gmail.com',
         'address'=>'testing', 'age'=>'35']
      );
   }
}

Since 'Niketan Vaahi' doesn't exist, a new record is inserted.

+----+---------------+------------------+---------------------+---------------------+---------+------+
| id |    name       |      email       |    created_at       |      updated_at     | address | age |
+----+---------------+------------------+---------------------+---------------------+---------+------+
| 1  | Siya Khan     | siya@gmail.com   | 2022-05-01 13:45:55 | 2022-05-01 13:45:55 | xyz     | 20  |
| 2  | Rehan Khan    | rehan@gmail.com  | 2022-05-01 13:49:50 | 2022-05-01 13:49:50 | xyz     | 18  |
| 3  | Rehan Khan    | rehan@gmail.com  | NULL                | NULL                | testing | 20  |
| 4  | Rehan         | rehan@gmail.com  | NULL                | 2022-05-29 14:17:02 | abcd    | 50  |
| 5  | Nidhi Agarwal | nidhi@gmail.com  | NULL                | NULL                | abcd    | 20  |
| 6  | Ashvik Khanna | ashvik@gmail.com | NULL                | NULL                | oooo    | 16  |
| 7  | Viraj Desai   | viraj@gmail.com  | NULL                | NULL                | test    | 18  |
| 8  | Priya Singh   | priya@gmail.com  | NULL                | NULL                | test123 | 20  |
| 9  | Arbaaz        | arbaaz@gmail.com | 2022-05-29 14:11:09 | 2022-05-29 14:11:09 | testing | 35  |
| 10 |Niketan Vaahi  | niketan@gmail.com| NULL                | NULL                | testing | 35  |
+----+---------------+------------------+---------------------+---------------------+---------+------+

Conclusion

Laravel's updateOrCreate() and updateOrInsert() methods provide elegant solutions for upsert operations. Use updateOrCreate() with Eloquent models for better integration with model features, and updateOrInsert() with DB facade for raw database operations.

Updated on: 2026-03-15T10:13:45+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements