Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as PostSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.
Laravel gives command to create seeder in laravel. so you can run following command to make seeder in laravel application.
Create Seeder Command
php artisan make:seeder PostSeeder
after run above command, it will create one file PostSeeder.php on seeds folder. All seed classes are stored in the database/seeds directory.
Then you can write code of create admin user using model in laravel.
database/seeds/AdminUserSeeder.php
use Illuminate\Database\Seeder;
use App\Post;
class PostSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Post::create([
'title' => 'tile of post',
'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut...',
]);
}
}
There is a two way to run this seeder. i will give you both way to run seeder in laravel.
Way 1: Run Single Seeder
You need to run following command to run single seeder:
php artisan db:seed --class=PostSeeder
The --class option allows you to specify a seeder class to run individually.
Way 2: Run All Seeders
In this way, you have to declare your seeder in DatabaseSeeder class file. then you have to run single command to run all listed seeder class.
database/seeds/DatabaseSeeder.php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(PostSeeder::class);
}
}
Now you need to run following command for run all listed seeder:
php artisan db:seed
Now i think you will understand how seeding is work.
For more details see the Laravel documentation.