@shyann
To insert multiple emails in Laravel, you can follow these steps:
- Create a new migration by running the command php artisan make:migration create_emails_table.
- Open the migration file (located in database/migrations) and define the columns you want in the up method. For example, you can create a column for the email address:
1
2
3
4
5
|
Schema::create('emails', function (Blueprint $table) {
$table->id();
$table->string('email');
$table->timestamps();
});
|
- Run the migration by running the command php artisan migrate.
- Create a new model for the Email by running the command php artisan make:model Email.
- In your controller, you can insert multiple emails by using the create method of the model. For example:
1
2
3
4
5
6
7
|
use AppEmail;
$emails = ['example1@example.com', 'example2@example.com', 'example3@example.com'];
foreach ($emails as $email) {
Email::create(['email' => $email]);
}
|
- You can also insert multiple emails in bulk by using the insert method of the model. For example:
1
2
3
4
5
6
7
|
$emails = [
['email' => 'example1@example.com'],
['email' => 'example2@example.com'],
['email' => 'example3@example.com'],
];
Email::insert($emails);
|
- Remember to import the Email model at the top of your controller file to use it:
By following these steps, you can easily insert multiple emails in Laravel using Eloquent ORM.