BurgerPedia: A Complete Laravel 5 and AngularJS Tutorial with Bootstrap to Make it Pretty – Part 3

in AJAX/jQuery/Javascript/Laravel/PHP/Tutorials & Samples/Web Development

Part 3 – Seeding the Database Tables

Now that we’ve created the database tables, we are ready to pre-populate them with data. That’s when artisan’s database seeders come into play. Seeders allow you to populate your database with test data so that you can be up and running quickly. To do so, we will use the ‘make’ namespace to create a brand new seeder. The following artisan command will generate a hamburgers seeder:

	php artisan make:seeder HamburgersTableSeeder

Now if you go inside the database directory, under the seeds directory, you will see a file called HamburgersTableSeeder.php. Additionally, you will see that there is another file in the directory called DatabaseSeeder, which is the default seeder class containing a run() function. This is the function that we need to modify in order to include our newly created hamburgers seeder. We will add the following line in DatabaseSeeder:

   $this->call(HamburgersTableSeeder::class);

Now the file looks like so:

database-seeder-php

Next, we will modify our hamburger seeder to include randomly-generated hamburgers. To do this, we will use a library called Faker that comes prepackaged with your Laravel installation. Faker is a PHP library that generates fake data for you! All you have to do is create an instance of Faker and use it to generate fake data. Since it’s already installed in Laravel, we can start using Faker right out of the box. Once a Faker instance is created, you can use $faker->name to generate a random person’s name, or $faker->email to generate a random email. You can learn more about Faker at their official github page but here are some of the fake data that can be generated by Faker.

$faker->randomDigit;
$faker->numberBetween(1,100);
$faker->word;
$faker->sentence;
$faker->paragraph;
$faker->lastName;
$faker->city;
$faker->year;
$faker->domainName;
$faker->creditCardNumber;
$faker->region; 
$faker->bankAccountNumber;
$faker->cellNumber; 

We will use Faker to generate the random data that we need. Here is the contents of the run() function inside the HamburgersTableSeeder with the addition on Faker:

	public function run()
    {	
		$faker = Faker::create();
		
    	foreach (range(1,10) as $index) {
			$now = date('Y-m-d H:i:s', strtotime('now'));
	        DB::table('hamburgers')->insert([
	            'name' => $faker->word . ' Burger',
	            'author' => $faker->name,
	            'overview' => $faker->paragraph,
				'created_at' => $now,
				'updated_at' => $now
	        ]);
        }
	
    }

The contents of the file now looks like so:

use Illuminate\Database\Seeder;

use Faker\Factory as Faker;

class HamburgersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {	
		$faker = Faker::create();
		
    	foreach (range(1,10) as $index) {
			$now = date('Y-m-d H:i:s', strtotime('now'));
	        DB::table('hamburgers')->insert([
	            'name' => $faker->word . ' Burger',
	            'author' => $faker->name,
	            'overview' => $faker->paragraph,
				'created_at' => $now,
				'updated_at' => $now
	        ]);
        }
	
    }
}

Now, to run the seeder, we can use the ‘db’ namespace in artisan. The following command will run our base seeder class. Since we have modified this class to run the hamburgers seeder, it will run the seeder for the hamburgers table:

	php artisan db:seed

Seeders are pretty dumb, so if you run the command again, additional entries will be added into the database.

If you wanted to seed a specific class, you can choose the class with the –class option in artrisan like so:

	php artisan db:seed --class=HamburgersTableSeeder</p>

If we look at the database table for hamburgers, there will now be 10 newly created hamburgers. Check out the screenshot below..

seeded-burgers

We should go ahead and also create a seeder for the descriptions as well. To do so, type the following command

	php artisan make:seeder DescriptionsTableSeeder</p>

And add the following lines of code in the run() command of the DescriptionsTableSeeder

    public function run()
    {
		$faker = Faker::create();
		
    	foreach (range(1,50) as $index) {
			$now = date('Y-m-d H:i:s', strtotime('now'));
	        DB::table('descriptions')->insert([
	            'author' => $faker->name,
				'title' => $faker->sentence,
	            'description' => $faker->text,
				'hamburger_id' => $faker->numberBetween(1,10),
				'created_at' => $now,
				'updated_at' => $now
	        ]);
        }
    }

This function basically creates 50 hamburger descriptions and associates them to a random hamburger. The final file now looks like so:

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;

class DescriptionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
		$faker = Faker::create();
		
    	foreach (range(1,50) as $index) {
			$now = date('Y-m-d H:i:s', strtotime('now'));
	        DB::table('descriptions')->insert([
	            'author' => $faker->name,
				'title' => $faker->sentence,
	            'description' => $faker->text,
				'hamburger_id' => $faker->numberBetween(1,10),
				'created_at' => $now,
				'updated_at' => $now
	        ]);
        }
    }
}

Don’t forget to change the DatabaseSeeder.php file to include the DescriptionsTableSeeder class. So the entire file now looks like so:

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(HamburgersTableSeeder::class);
		$this->call(DescriptionsTableSeeder::class);
    }
}

Finally, if you want to refresh the database and seed as well, you can run the following artisan command which will completely re-build your database and reseed it with random data:

	php artisan migrate:refresh --seed</p>

Previous Step: Creating the Database Tables using Migrations
Next Step: Creating the Models and the Relationships

Tutorial Contents
Tutorial Resources

Mifty Yusuf is a Montreal-based software developer who enjoys playing with new web technologies as well as comic books and illustrations. He beleives that, no matter what the question is, the answer is always Batman!

Leave a Reply

Your email address will not be published.

*

Latest from AJAX

Go to Top