Laravel 7/6 updateOrCreate() Query Example

10-Apr-2023

.

Admin

Hi Guys,

In this blog,I will explain how can use laravel provides an updateorcreate method.we are this updateOrCreate method to also database create new recored or recored is already exists then update recored. I will show laravel updateorcreate() query example.

You may also come across situations where you want to update an existing model or create a new model if none exists. Laravel provides an updateOrCreate method to do this in one step.

Here the example of updateOrCreate method.

Example


you can learn following updateOrCreate mehtod example.i will show table recoreds before updateorcreate method use.

Here before updateorcreate method use in table recoreds

Current User Database Table

blow the updateorcreate method to create recoreds

Create Recoreds Using updateorcreate Method

here updateorcreate Method using create recoreds.

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index(){

$user = ["name" => "Pqr",

"email" => "Pqr@gmail.com",

"password" => bcrypt('123456'),

"country" => "india",

];

$userInformation = ["state" => "Gujarat","temperature" => "10"];

User::updateOrCreate($user,$userInformation);

dd("suucessfull");

}

Create Recored User Database Table

After Create Recores database recored table

Update Recoreds Using updateorcreate Method

here updateorcreate Method using update recoreds.

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index(){

$user = ["name" => "Pqr",

"email" => "Pqr@gmail.com",

"password" => bcrypt('123456'),

"country" => "india",

];

$userInformation = ["state" => "Gujarat","temperature" => "16"];

User::updateOrCreate($user,$userInformation);

dd("suucessfull");

}

Update Recored User Database Table

After update Recores database recored table

It will help you...

#Laravel 7

#Laravel

#Laravel 6