How to create and update Laravel Eloquent?


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   | 
+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+

Example1

Using method updateOrCreate()

We are going to make use of the method updateOrCreate() to insert or update the item inside the student table. This method will take care of either inserting the record if it does not exist or it will update the record if it already exists.

Syntax

The syntax for updateOrCreate() is as follows 

$flight = Model::updateOrCreate(
   ['field1' => 'value'],
   [field=>value, field1=>value]
);

The first value in the array is used to search in the table if it exists, and if not it will insert the value or it will update for the match of the first parameters in the array.

Let us try an example using it.

<?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 the value is inserted in the table.

Output

The output of the above code is −

+----+---------------+------------------+---------------------+---------------------+---------+------+ 
| 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

Update using method updateOrCreate()

We tried to insert a value since it did not exist, let us now try the example to update the record in the table.

<?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] ); } }

In the above example, we are trying to update the age of 50 for the name Rehan.

Output

The output of the above code is −

+----+---------------+------------------+---------------------+---------------------+---------+------+
| 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   |
+----+---------------+------------------+---------------------+---------------------+---------+------+

Example 3

Using updateOrInsert() with DB Facade to update.

To make use of the DB facade you need to use the class: use Illuminate\Support\Facades\DB; or just use DB. DB Facade is a query builder tool used in Laravel. Using it you can try all the SQL queries.

<?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']); } }

In the above case, it will search in the table for the key/values pairs given in the first params, if there is a matching record the same will be updated or the insert will take place.

Output

The output of the above code is

+----+---------------+------------------+---------------------+---------------------+---------+------+
| 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   |
+----+---------------+------------------+---------------------+---------------------+---------+------+

Example 4

Using updateOrInsert() with DB Facade to insert.

Here we are checking if the name: Niketan Vaahi is present in the table, if not the record with details given in the second param will be inserted.

<?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']); } }

Output

The output of the above code is −

+----+---------------+------------------+---------------------+---------------------+---------+------+
| 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  |
+----+---------------+------------------+---------------------+---------------------+---------+------+

Updated on: 30-Aug-2022

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements