Database Seeding

Submitted by Clivern - 10 years ago

With database seeding you can fill your database with dummy data with simple command. I created code that will fill users table with some data. users table consists of three columns (username,email,biog).

//app/database/seeds/DatabaseSeeder.php
class DatabaseSeeder extends Seeder {

    /** 
     * Run the database seeds.
     *
     * @return void
     */
      public function run()
      {
    Eloquent::unguard();

        //call uses table seeder class
	$this->call('UsersTableSeeder');
        //this message shown in your terminal after running db:seed command
        $this->command->info("Users table seeded :)");
       }

}

class UsersTableSeeder extends Seeder {
 
       public function run()
       {
         //delete users table records
         DB::table('users')->delete();
         //insert some dummy records
         DB::table('users')->insert(array(
             array('username'=>'john','email'=>'john@clivern.com','biog'=>'PHP Ninga'),
             array('username'=>'mark','email'=>'mark@clivern.com','biog'=>'JS Ninga'),
             array('username'=>'Karl','email'=>'karl@clivern.com','biog'=>'Jquery Ninga'),
             array('username'=>'marl','email'=>'marl@clivern.com','biog'=>'Not Ninga'),
             array('username'=>'mary','email'=>'mary@clivern.com','biog'=>'HTML Ninga'),
             array('username'=>'sels','email'=>'sels@clivern.com','biog'=>'CSS Ninga'),
             array('username'=>'taylor','email'=>'taylor@clivern.com','biog'=>'Laravel Ninga'),

          ));
       }
 
}

//navigate to laravel root with terminal and run the following command
$php artisan db:seed