Seeders allow you to populate your database with initial or sample data. This is especially useful during development or testing stages when you need to have a consistent set of data for your application. With seeders, you can define data in a structured and repeatable manner, making it easy to populate tables with default records.
Step1 :Create Seeder Class:
php artisan make:seeder AdminUserSeeder
database/seeders/AdminUserSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
class AdminUserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
User::create([
'name' => 'Hardik',
'email' => 'admin@gmail.com',
'password' => bcrypt('123456'),
]);
}
}
There is a two way to run this seeder
Way 1: Run Single Seeder
php artisan db:seed --class=AdminUserSeeder
Way 2: Run All Seeders
In this way, you have to declare your seeder in the DatabaseSeeder class file. then you have to run a single command to run all listed seeder class.
database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call(AdminUserSeeder::class);
}
}
Now you need to run following command for run all listed seeder:
php artisan db:seed
See Output in your database check users table. Thank You