In this video we are going to learn about Database Seeding.
Laravel includes a simple method of seeding your database with test data using seed classes.
Seed classes may have any name,
By default, a Database Seeder class is defined for you.
From this class, we can use the call method to run other seed classes and also allowing us to control the seeding order.
So Lets create a Seeder.
For that switch to the command prompt and here type.

php artisan make:seeder PostTableSeeder

Here PostTableSeeder is seeder name.
This PostTableSeeder is now created inside the seeds directory.
Lets check.
So go to the project and click on database.
Then seeds.
You can see here PostTableSeeder Inside the post table seeder class only contains one method by default which is run method.
This method is called when the db:seed artisan command is executed.
Within the run method, we can write code for insert data into the database.
Let's modify the PostTableSeeder class and add a database insert statement inside the run method.


DB::table(posts')->insert([
'title' => 'Fitst post',
'body' => 'First post description',
]);


Also import DB befor the class.


use Illuminate\\Support\\Facades\\DB;


Now save the file Now call this post seeder calss inside the DatabaseSeeder run method,
So just type here.

public function run()
{
$this->call([
PostsTableSeeder::class,
]);
}

Lets Run this Seeders.
Before executing seeders regenerate Composer's autoload.
So just goto the command prompt and run the command.

composer dump-autoload


Now we can use the db:seed Artisan command to seed the database.
By default, the db:seed command runs the DatabaseSeeder class which may be used to call other seed classes.
Before using this seed artisan command lets check the posts table.
So goto the phpmyamdin and here you can see inside the posts table there is no any record.
Now ,execute db:seed artisan command in command prompt.

php artisan db:seed


Now command is executed.
So lets check the posts table.
Alright now you can see the record.
Now lets seed one more record.
Go to the PostTableSeeder.
Add here just change the post title and body.
Now inside the command prompt.

php artisan db:seed


Now go to phpmyadmin and refresh the posts table.
You can see another record .
We can also seed multiple records at a time so just goto the postseeder.
And here I am going to use faker package so before the class import the faker.

use Faker\\Factory as Faker;


Inside the run method, just type here.

public function run() {
$faker = Faker::create();
foreach(range(1, 100) as $index) {
DB::table(posts')->insert([
'title' => $faker->sentence(5),
'content' => $faker->paragraph(4)
]);
}
}
Now run this seeder.
For that go to the command prompt and here type.

php artisan db:seed

Lets check post table, go to the phpmyadmin.
And just refresh the posts table.
You can see here hundreds of records are inserted here.
You may also seed your database using the migrate:fresh command,
which will drop all tables and re-run all of your migrations.
This command is useful for completely re-building your database:.
So lets check this command,
Goto the command prompt and here type.

php artisan migrate:fresh –seed

Now you can see here all tables are recreated and you can also see the records are re-inserted inside the post table.
So, in this way you can use the database seeder in laravel 8.