Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel DB Seeds - Test Data v Sample Data

I'm probably misunderstanding exactly how this works, but what's the best way to accomplish this? I have something in mind but it seems quite hacky.

I have a set of sample data which I use to test my application. This is seeded via the built in seeder in Laravel. This contains things like example users, addresses, documents etc.

I also have a set of default data which should go in production. I currently add this directly in the migration. For example, if I was adding a table for account_roles, I might include the following at the bottom of the migration

$account_admin = array('role' => 'Account Administrator', 'flag' => 'ACCOUNT_ADMIN');
$account_owner = array('role' => 'Account Administrator', 'flag' => 'ACCOUNT_OWNER');
DB::table('account_roles')->insert($account_admin);
DB::table('account_roles')->insert($account_owner);

This way, on production, I just migrate the database to insert any production ready database values, and on staging/development, I can refresh the migrations and then seed the database with sample data.

Is there any other (better) way to do this?

like image 615
Alex C Avatar asked Sep 23 '13 01:09

Alex C


People also ask

What is the difference between seeder and factory in Laravel?

Database seeder is used to populate tables with data. Model factories is a convenient centralized place to define how your models should be populated with fake data.

What is data seeding testing?

Preparing data in your application for testing is called “seeding.” For example, if you have a test that creates a report, you need data to display and verify. Or, if you want testers to log in, you need test accounts with login credentials. Seed your environment to make sure it is ready to be tested.

What is data seeding in Laravel?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Why we use seeding in Laravel?

Laravel offers a tool to include dummy data to the database automatically. This process is called seeding. Developers can add simply testing data to their database table using the database seeder. It is extremely useful as testing with various data types allows developers to detect bugs and optimize performance.


1 Answers

You could run a check on the current environment in your seeder file, and seed as needed

<?php

class DatabaseSeeder extends Seeder {

    public function run()
    {
            Eloquent::unguard();

            if (App::environment() === 'production')
            {
                $this->call('ProductionSeeder');
            }
            else
            {
                $this->call('StagingSeeder');
            }
    }

}
like image 160
Laurence Avatar answered Sep 28 '22 08:09

Laurence