Laravel create method 1

Laravel create method

// If there's a flight from Oakland to San Diego, set the price to $99.
// If no matching model exists, create one.
$flight = App\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99, 'discounted' => 1]
);

Here is what the above code is Doing:
1. The updateOrCreate method will attempt to locate a database record using the first argument’s column and value pairs.
2. If a model is found, it will be updated with the second argument’s column and value pairs.
3. If no model is found, one will be inserted with the first argument’s and second argument’s column and value pairs.

The updateOrCreate method accepts an array of conditions as its first argument, and an array of column and value pairs as its second argument.

The updateOrCreate method will set the updated_at column to the current date and time.

The updateOrCreate method will return the model instance that was inserted or updated.

The updateOrCreate method will set the updated_at column to the current date and time.

The updateOrCreate method will return the model instance that was inserted or updated.

Similar Posts