In this article, we will see the laravel 8 database seeder example. As we all know laravel framework provides many functionalities to the user to reduce the developer's time for developing a website. Here, we will see how to create database seeder in laravel 8.
Many times you have a requirement to add some default records or entries to log in or add form details etc. there is no problem adding manual data in the database one or two times but it is a very challenging task to add data manually each and every time. So, if you want to avoid this boring task you need to create database seeders in laravel, laravel provides an inbuilt command to create database seeders in laravel 8.
So, let's see how to create seeder in laravel 8 and laravel 8 seeder multiple records.
So, run the following command in your terminal to create a seeder in the laravel application.
php artisan make:seeder UserSeeder
Now, it will create one file UserSeeder.php in the seeds folder. All seed classes are stored in the database/seeds directory.
Add your data as per your requirements like below in this path database/seeds/UserSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Models\User;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'name' => 'test',
'email' => '[email protected]',
'password' => bcrypt('123demo'),
]);
}
}
Now, run the below command in your terminal.
php artisan db:seed --class=UserSeeder
You can declare your seeder in the DatabaseSeeder class file. then you have to run a single command to run all listed seeder classes.
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UserSeeder::class);
}
}
and run the below command in your terminal.
php artisan db:seed
You might also like:
- Read Also: Laravel 9 Barcode Generator Example
- Read Also: Create Dummy Data Using Laravel Tinker
- Read Also: How To Get Last 7 Days Record In Laravel 8
- Read Also: Laravel 9 Create Middleware For XSS Protection