create if not exist laravel

Solutions on MaxInterview for create if not exist laravel by the best coders in the world

showing results for - "create if not exist laravel"
Mario
25 Feb 2018
1if(!Numbers::where('country',$country)->exists()){
2    Numbers::Create([
3      'country'    => $country
4      ]);
5}
Daniele
08 Nov 2019
1Occasionally, you may need to update an existing model or create a new model if
2no matching model exists. Like the firstOrCreate method, the updateOrCreate 
3method persists the model, so there is no need to manually call the save method.
4
5In the example below, if a flight exists with a departure location of Oakland 
6and a destination location of San Diego, its price and discounted columns will
7be updated. If no such flight exists, a new flight will be created which has 
8the attributes resulting from merging the first argument array with the 
9second argument array:
10
11$flight = Flight::updateOrCreate(
12    ['departure' => 'Oakland', 'destination' => 'San Diego'],
13    ['price' => 99, 'discounted' => 1]
14);