How to Create Seeder in Laravel with Example

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023


In this tutorial we will learn how to create seeder in laravel with example.

In Laravel, seeding is the process of inserting initial data into a database. Seeders are classes that allow you to populate your database with test data or default values.


How to create seeder in laravel with example

To create a seeder in Laravel, you can follow these steps:


Step 1 - Generate a Seeder Class

php artisan make:seeder UsersTableSeeder


Step 2 - Define the Seeder Class Open the newly created `database/seeders/UsersTableSeeder.php` file and define the `run()` method. This method will contain the code that inserts data into the database.



namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;

class UsersTableSeeder extends Seeder
     * Run the database seeds.
     * @return void
    public function run()
        // Insert default users
            'name' => 'John Doe',
            'email' => '',
            'password' => bcrypt('password'),
            'name' => 'Jane Doe',
            'email' => '',
            'password' => bcrypt('password'),


Step 3 - Call the Seeder Class To run the seeder, you can use the following command:

php artisan db:seed --class=UsersTableSeeder

This will insert the default users into the database.


You can create multiple seeders to populate different tables in your database. Just make sure to define unique class names and call them using the `--class` option.


Note: Remember to include the `use` statement for any model that you want to insert data into.

Comments 0

Leave a comment