php

Laravel 9 UpdateOrCreate Method With an Example

“When any developer creates an application, then he/she needs to flow some major side of the application. One of them is data overlapping. To stop data overlapping, developers need to flow some techniques. For example, fast need to check whether data has existed in the data table or not. If it exists, then get the data and update it; else, it will create new data in the table. Laravel has a great future to do this job. This method is called the updateOrCreate method. It will do this job easily.”Today we will explain how to updateOrCreate Method work with an example.

Project requirements Are given below

  • MySQL 8.0+
  • MariaDB 10.2+
  • PHP 8.1

Here is an example of defining this updateOrCreate

Process 1. Create a UpdateOrCreate Project

Now, we need to run this command to create the UpdateOrCreate project

Composer create-project Laravel/Laravel UpdateOrCreate

Process 2. Database Connection

Open the .env file on the UpdateOrCreate project and add a new database name, username, and password

Code-

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=Database name

DB_USERNAME=Database user name

DB_PASSWORD=Database password

The database Looks like this

Once the database is connected next, go to the next step.

Process 3. Create a Model and Controller in the UpdateOrCreate Project

Now, we will create a model and controller for our project. For that, we need to run this command

php artisan make:model newRec -mc

After running this command, it will create two files in our project. One is a controller located in “app\Http\Controllers\NewRecController.php”

Another is “database\migrations\2022_07_11_042129_create_new_recs_table.php”

Need to add these two lines to the data migration file

$table->string('name')->nullable();
$table->string('price')->nullable();

After adding this, it looks like

public function up()

{

Schema::create('new_recs', function (Blueprint $table) {

$table->id();

$table->string('name')->nullable();

$table->string('price')->nullable();

$table->timestamps();

});

}

Now, need to run this command to migrate the table into the database

php artisan migrate

Process 4. UpdateOrCreate Method Create and Apply

Normally we use this way to find and update data or create data.

Using the first() method, we check whether the data exists or not. If it exists, then we make it update else to create the new data.

Here is the code looks like

class NewRecController extends Controller

{

public function checkandadd(){

$dataName = "Domain";

$price = "11";

$inputdata['name'] = $dataName;

$inputdata['price'] = $price;

$check = newRec::where('name',$dataName)->first();

if($check == null){

newRec::create($inputdata);

}else{

$check->fill($inputdata)->save();

}

}

}

This is the old way to check and insert or update data in the database.

Now we will see how to updateOrCreate look like in the code with the same code

class NewRecController extends Controller

{

public function checkandupdate(){

$dataName = "Domain";

$price = "11";

newRec::updateOrCreate(

[ 'name' => $dataName ],

[ 'price' => $price ]

);

}

}

The code looks short and simple.

Let’s create a route to check this method.

Route::get('/createOrUpdate', [NewRecController::class, 'checkandupdate'])->name('checkandupdate');

Process 5. Run and Test the UpdateOrCreate Project

For testing, the project needs to run this command

php artisan serve

Need to check whether the code is working or not, to the route for checking

Yes, it’s working result in the database

We will add price 111 to our function

public function checkandupdate()

{

$dataName = "Domain";

$price = "111";

newRec::updateOrCreate(

['name' => $dataName],

['price' => $price]

);

}

And run the route again. Then check data was updated or not

Yes, it’s updated successfully.

Consolation

Finally, we created this Laravel UpdateOrCreate project with Laravel 9. Creating a data table Laravel using UpdateOrCreate is very useful. Hope this UpdateOrCreate project example will help you to understand the updateOrCreate in Laravel 9.

About the author

Rakhibul Hasan

This is Rakhibul Hasan CEO and Founder of Clipping Path Creative Inc. We provide 100% handmade photo editing services to use Adobe Photoshop. Our services are Clipping path, Background removal, Color correction, Photo retouching, Car photo editing, drop shadow etc. Also we offer an image as a trial to judge our quality. So contact us and get instant reply.