Sirjan Sharma
Sirjan Sharma

Reputation: 79

Laravel: Quickly seed database tables

I need to quickly seed database tables. I have 23 tables including pivot tables and excluding password_resets and migrations. Most of them take part in relationship.

Since there are 23 tables to seed, writing a seeder class is not quick. Is there something I can use that will seed the tables based on data types and length taking into account the foreign keys. Right now, it doesn't matter if the data makes no sense. For eg, name could be filled with "lasdkjflk alfsd". However, it should be a string of <20 characters if that is how the table is designed.

Upvotes: 2

Views: 886

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Try to use a model factories that provide a fake data, take a look to Laravel 5.2 Model Factories.

Also take a look to Faker seeds,e.g :

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      $faker = Faker::create();
      foreach (range(1,5) as $index) {
        DB::table('posts')->insert([
            'title' => $faker->catchPhrase,
            'content' => $faker->paragraph,
            'created_at' => $faker->dateTime($max = 'now'),
            'updated_at' => $faker->dateTime($max = 'now'),
        ]);
      }
    }
}

Hope this helps.

Upvotes: 1

Related Questions