Laravel 8 Database Seeder Example

Websolutionstuff
2 min readMar 15, 2021

--

Today we will see Laravel 8 Database Seeder Example, as we all know laravel framework provide many functionalities to user to reduce developer’s time for developing website, here we will see How to create database seeder in Laravel 8.

Many times you have requirement to add some default records or entry to login or add form details etc. there are no problem to add manual data in database one or two times but it is very challenging task to add data manually each and every time.

So, if you want to avoid this boring task you need to create database seeders in laravel, laravel provides inbuilt command to create database seeders in laravel 8.

So, run following command in your terminal to create seeder in laravel application.

php artisan make:seeder UserSeeder

Now it will created one file UserSeeder.php on seeds folder. All seed classes are stored in the database/seeds directory.

Add your data as per your requirements like below in this path database/seeds/UserSeeder.php

<?phpuse Illuminate\Database\Seeder;
use App\Models\User;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'name' => 'test',
'email' => 'admin@gmail.com',
'password' => bcrypt('123demo'),
]);
}
}

Now run below command in your terminal

php artisan db:seed --class=UserSeeder

Or you can declare your seeder in DatabaseSeeder class file. then you have to run single command to run all listed seeder class.

<?phpuse Illuminate\Database\Seeder;class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UserSeeder::class);
}
}

and run below command in your terminal.

php artisan db:seed

--

--

Websolutionstuff
Websolutionstuff

Written by Websolutionstuff

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API. Visit Website: http://websolutionstuff.com/

No responses yet