Hello Devs, 

In this tutorial, we will learn Laravel 7/6 updateOrCreate() Query Example

If you want to update an existing model or if you want to create a new model then laravel provides an UpdateorCreate method to do this in one step only. 

Follow this step by step guide below. 



Example:

Create Recoreds Using updateorcreate Method

/**
 * 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

Update Recoreds Using updateorcreate Method

/**
 * 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");
}


May this example help you.