How Do We Save A Model In Laravel?

How Do We Save A Model In Laravel?

How Do We Save A Model In Laravel?

– To save a model in Laravel, you can use the `save` method on an instance of the model.

Example:

$user = new User;
$user->name = 'John Doe';
$user->email = '[email protected]';
$user->save();

This will insert a new record into the user's table with the specified name and email.

– You can also use the `create` method on the model.

Example:

$user = User::create([
    'name' => 'John Doe',
    'email' => '[email protected]',
]);

This method will insert a new record into the database and will return an instance of the created model.

To use the `create` method, you must have the `$fillable` property with appropriate fields defined in your model.

Example:

class User extends Model
{
    protected $fillable = ['name', 'email'];
}
All Comments