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
- Process 2. UpdateOrCreate project Database connection
- Process 3. Create a model and controller in the UpdateOrCreate project
- Process 4. UpdateOrCreate method create and apply
- Process 5. Run and test the UpdateOrCreate project
Process 1. Create a UpdateOrCreate Project
Now, we need to run this command to create the UpdateOrCreate project
Process 2. Database Connection
Open the .env file on the UpdateOrCreate project and add a new database name, username, and password
Code-
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
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('price')->nullable();
After adding this, it looks like
{
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
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
{
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
{
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.
Process 5. Run and Test the UpdateOrCreate Project
For testing, the project needs to run this command
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
{
$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.