DEV Community

Cover image for Create DB Seeder in Laravel
Sayandeep Majumdar
Sayandeep Majumdar

Posted on

Create DB Seeder in Laravel

In Laravel, seeding the database allows you to populate your database tables with sample or default data. This is particularly useful during development and testing when you need to have consistent data for your application. You can create seeders that define the data you want to insert into the database.

To create a seeder in Laravel, you can use the artisan command-line tool. Follow these steps:

1. Create a new seeder:

Open a terminal or command prompt and run the following command:

php artisan make:seeder ExampleTableSeeder

Enter fullscreen mode Exit fullscreen mode

This will create a new seeder file in the database/seeders directory.

2. Edit the seeder file:

Open the created seeder file (e.g., ExampleTableSeeder.php) located in the database/seeders directory. The file will look something like this:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ExampleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Insert your data here using the DB facade
        // For example:
        // DB::table('example_table')->insert([
        //     'column1' => 'value1',
        //     'column2' => 'value2',
        // ]);
    }
}

Enter fullscreen mode Exit fullscreen mode

3. Define the data to be inserted:

In the run()method of the seeder file, use the DB facade to insert the data into the respective database table. You can insert multiple rows using a loop or insert them individually.

4. Load the seeder:

To seed the database, you need to call the seeder from the DatabaseSeeder class. This class is defined in the database/seeders directory and is automatically generated when you create a new Laravel project.

Open the DatabaseSeeder.php file and add the call to your ExampleTableSeeder in the run() method:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(ExampleTableSeeder::class);
        // Add more seeders here if needed
    }
}

Enter fullscreen mode Exit fullscreen mode

5. Run the seed command:

Now, you can run the following command to seed the database:

php artisan db:seed

Enter fullscreen mode Exit fullscreen mode

This command will execute the run() method of the DatabaseSeeder class, which, in turn, will call your ExampleTableSeeder and insert the defined data into the database table.

Remember that each time you run the seed command, it will refresh the data in your database, so use it with caution, especially in a production environment. In a production environment, seeding is typically not needed, and you should be using migrations and a database seeder for development and testing purposes.

#DevelopersLab101 #LaravelDevelopment

Top comments (0)