Table of Contents

Database Seeds

Database Seeds

A zombie developer imports data into their database with an SQL data dump, whereas a Laravel developer uses Seeds to input default data into their application.

If you were to build an app that does not have any data, it would look pretty useless. Luckily we can leverage seeds, which allow us to add default data into our database schemas. So, when we hand over our app to another developer we can always make sure there is a little bit of seed data to work with.


Seeds

Laravel allows us to add test data into our application. This is referred to as seeding the database. In the previous section we talked about migrations and in this section we will talk about seeds which is the data stored in our database.

Let's work off of our previous examples where we had a zombie table. Inside the database we had 2 test zombies that were:

  • Johnny Bullet Holes
  • Ted Manwalking

If we have our database migration for our zombies table we could hand over our application to another developer and they can run php artisan migrate and be up and running with the zombies table. Well, what if we also wanted to include our zombie data in the database?

We can create a Zombie Table Seeder for that.

Let's go ahead and use our friend artisan again:

$ php artisan make:seeder ZombieTableSeeder

After running this command we will see a new file created at database\seeds\ZombieTableSeeder.php, and the contents of that file will look similar to the following:

<?php

use Illuminate\Database\Seeder;

class ZombieTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    }
}

The function that will run when we want to seed the database is the run() method, and this is where we will put our code. To seed our zombie database with our two zombies we would add this to our run method:

public function run()
{
    $zombies = array(
        ['name' => 'Johnny Bullet Holes', 'strength' => 'Strong', 'health' => 70],
        ['name' => 'Ted Manwalking', 'strength' => 'Weak', 'health' => 90]);
    DB::table('zombies')->insert( $zombies );
}

What we have done is create an array of zombies containing their name, strength, and health. We then use the DB class to insert our zombies into the Zombies table. To run this seeder we will run the following artisan command:

$ php artisan db:seed --class=ZombieTableSeeder

And now if we look in our database we will have our 2 zombies in our zombies table.

Now we can use version control for our data, so when a new developer pulls a fresh copy of our app on their computer they will also have the data from our database.

Gone are the days of passing around an SQL file and now are the days of creating migrations and seeds.

From here on out, feel free to chant this mantra:

ALL HAIL MIGRATIONS AND SEEDS!

This is just another example of how Laravel makes our lives easier. Let's move on to learning about the built-in security that Laravel provides.