Seeder can create some fake data in table, it’s useful.

  • Create seeder
1
php artisan make:seeder ArticleSeeder

After that a file named ArticleSeeder.php would be created under database/seeds. Change function run as follow:

1
2
3
4
5
6
7
8
9
10
11
12
public function run()
{
DB::table('articles')->delete();

for ($i=0; $i < 10; $i++) {
\App\Article::create([
'title' => 'Title '.$i,
'body' => 'Body '.$i,
'user_id' => 1,
]);
}
}
  • Register seeder

Then we need register ArticleSeeder in project. Modify function run in file database/seeds/DatabaseSeeder.php as follow:

1
2
3
4
public function run()
{
$this->call(ArticleSeeder::class);
}
  • Excute seed and create fake data

Excuste seed with command

1
php artisan db:seed

. You can get the fake data.