Laravel

Insert data using Database Seeder in Laravel

January 30, 2018

author:

Insert data using Database Seeder in Laravel

There are numerous instances where we need a populated database in order to test several operations. Many developers add these data rows manually. But with Laravel framework, you will never have to do that all by yourself. Laravel uses seed classes for seeding database with test data. All these classes are stored in database/seeds directory.

# Write Seeder Example

We can generate a seeder file with artisan command:

php artisan make:seeder PostsTableSeeder
# database/seeds/PostsTableSeeder.php

<?php

use Illuminate\Database\Seeder;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      //
    }
}

A seeder class has a default method named run(). Within this method, we can populate database with data. Let us try and create seeders for posts table

# database/seeds/PostsTableSeeder.php
...
public function run()
{
   DB::table('posts')->insert([
       'title' => 'What is Laravel',
       'content' => 'Laravel is a PHP framework...',
       'category' =>'Laravel',
   ]);
}

We can also populate multiple data:

# database/seeds/PostsTableSeeder.php
...
public function run()
{
   DB::table('posts')->insert(array(
     array(
       'title' => 'Migration and Seeder',
       'category' => 'Laravel',
     ),
     array(
       'title' => 'Adding multiple data in seeder',
       'category' => 'Laravel',
     ),
   ));
}

# Call Seeder Example

Like many other features in Laravel, just creating seeders won’t populate the database, two more steps are involved. Calling the seeders and running them.

Laravel provides default seeder class named DatabaseSeeder within database/seeds which is called by default with the db:seed command. It can be used to call user-defined seeders like this:

# database/seeds/DatabaseSeeder.php
...
public function run()
{
    $this->call([
        PostsTableSeeder::class
    ]);
}

# Call Seeder from Route & Controller

The usual way to call seeder is through the laravel artisan command which we will discuss in a moment. However, at times, you may also need to call seeder programmatically from route or controller file. Here’s how that can be achieved:

// Route or Controller file

\Artisan::call('db:seed');

OR

\Artisan::call('db:seed', array('--class' => 'PostsTableSeeder'));

# Call Seeder from Migration

Further, you may also have a possibility to call the seeder from laravel migration. It is called the same way as we did above:

\Artisan::call('db:seed');

# Run Seeder Example

The final step to seed the database is to call it with artisan command:

php artisan db:seed

Note that we can also run a particular database seeder class directly without adding it to the DatabaseSeeder class by:

php artisan db:seed --class=PostsTableSeeder

Additionally, we can seed the database while refreshing migrations which rollback the old migrations and creates fresh copies with:

php artisan migrate:refresh --seed

Conclusion:
Laravel Seeding helps developers save some crucial time. And they become even more fruitful when a project has a large team. Imagine if each developer creates his own dummy repository in the local database to perform several operations. A huge amount time would be wasted. But with this feature, one developer can create seeders and the whole team can use the same chunk of repository for their testing. Such features help in project enhancement.

Questions & Comments:

Thanks for stopping by. If you run into issues while working with seeders in Laravel, drop them in the comment section below.

Leave a comment

Your email address will not be published. Required fields are marked *