Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/creating-a-seeder-in-laravel-8 In this post, I will share an example of how to create a Laravel 8 seeder.
Open the CreatePostsSeeder.php and you will see the following code: 'Post 1', 'description' => 'Description for post 1', 'body' => 'Body for post 1' ]); Post::create([ 'title' => 'Post 2', 'description' => 'Description for post 2', 'body' => 'Body for post 2' ]); Post::create([ 'title' => 'Post 3', 'description' => 'Description for post 3', 'body' => 'Body for post 3' ]); Post::create([ 'title' => 'Post 4', 'description' => 'Description for post 4', 'body' => 'Body for post 4' ]); Post::create([ 'title' => 'Post 5', 'description' => 'Description for post 5', 'body' => 'Body for post 5' ]); }}
Now let's save the data by running the following commands below: php artisan db:seed or in a specific command for a seeder class: php artisan db:seed --class=CreatePostsSeeder
The migrate:refresh --seed is a shortcut of the following commands below: php artisan migrate:reset # rollback all migrations php artisan migrate # run migrations php artisan db:seed # run seeders