Using Laravel Seeders to Populate a Database with Test Data

This guide explains how to use Laravel seeders to generate test data, covering creation of seeder classes via Artisan, inserting records manually or with model factories, calling additional seeders, and the commands needed to run or refresh database seeding.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using Laravel Seeders to Populate a Database with Test Data

Laravel provides seed classes that make it easy to populate a database with test data. All seed classes are stored in the database/seeds directory, and you should follow naming conventions such as UsersTableSeeder.

To create a new seeder, run the Artisan command: php artisan make:seeder UsersTableSeeder Each generated seeder contains a single run method, which is called when the db:seed command is executed. Inside run you can insert data manually using the query builder or Eloquent models.

Example of a DatabaseSeeder that inserts a user record:

<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => str_random(10),
            'email' => str_random(10) . '@gmail.com',
            'password' => bcrypt('secret'),
        ]);
    }
}

Instead of manually setting each attribute, you can use model factories to generate large amounts of data quickly. For example, to create 50 users and associate posts with each:

/**
 * Run the database seeds.
 */
public function run()
{
    factory(App\User::class, 50)->create()->each(function ($u) {
        $u->posts()->save(factory(App\Post::class)->make());
    });
}

In the DatabaseSeeder you can call other seeders using the call method, which keeps each seeder file small and organized:

/**
 * Run the database seeds.
 */
public function run()
{
    $this->call([
        UsersTableSeeder::class,
        PostsTableSeeder::class,
        CommentsTableSeeder::class,
    ]);
}

After writing your seeders, refresh Composer's autoloader: composer dump-autoload Then run the seeder with: php artisan db:seed To run a specific seeder, use the --class option: php artisan db:seed --class=UsersTableSeeder You can also rebuild the database and seed it in one step using:

php artisan migrate:refresh --seed
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backenddatabaseTestingPHPLaravelSeeder
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.