How to create and store a new record in the database using Eloquent in Laravel?

To create and store a new record in the database using Eloquent in Laravel, you can follow these steps:

  1. Create a new instance of your Eloquent model:
$newRecord = new YourModel;
  1. Assign values to the model properties:
$newRecord->property1 = 'value1'; $newRecord->property2 = 'value2'; // ...
  1. Save the record to the database:
$newRecord->save();

This will insert a new record into the corresponding table in the database. Laravel automatically handles generating the necessary SQL statements to perform the insert operation.

Alternatively, you can use the create method on your model to accomplish the creation and storage in a single step.

$newRecord = YourModel::create([ 'property1' => 'value1', 'property2' => 'value2', // ... ]);

This method will automatically create a new record in the database and return the created object.

Make sure to replace YourModel with the actual name of your Eloquent model class, and property1, property2, etc. with the actual names of your model's properties.