Using Factories to seed your database with Laravel

I have recently explored factories for seeding my database. Previously I looked at seeding a database in a way which is very simple but necessary for my needs.  I also needed some dummy data on bulk to save a little time so I have used a factory to do this.

So I needed to add some dummy data to a table but I needed it to follow some specific rules.  This is what I did.

Open database\factories\ModelFactory.php

$factory->define(App\FrameworkOutcome::class, function (Faker\Generator $faker) {
    return [
        'framework_id' => rand(1,3),
        'category_id' => rand(1,5),
        'outcome' => $faker->sentence(10),
        'order' => 0,
        'active' => 0,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' =>  date('Y-m-d H:i:s')
    ];

});

There is a factory for users already set up and ready to go.  It is pretty self explanatory but I will expand a little on what I did.  If you want you can skip down to where I add in the call within the DatabaseSeeder.php to get it to run.

In the code above and for your needs you can override any details you want by passing an array for your table contents.

The FrameworkOutcome model I am working with relates to two other tables Framework and Categories.  Suffice it to say that I have populated these tables but to make my outcomes relevant to these other tables I have had to specify certain attributes such as 'framework_id' needs an integer of 1, 2 or 3.  I used rand(1,3) to achieve this and the same for ‘category_id’.

I used $faker->sentence with 10 words in the sentence. There are a bunch of faker formatters which can be found on the Github page.

Now once this is all set up, go to database\seeds\DatabaseSeeder.php and add the following line in the ‘run’ method.

factory(FrameworkOutcome::class, 20)->create();

Run the seeder: php artisan db:seed

Twenty records with random data within the specified guidleines will be added to the database.

Laravel database seeding

This is a very simple example of Laravel’s seeding to populate a new database with some data.

I often rollback and refresh my database migrations while I am developing in Laravel.  The thing is I generally need to have at least a user in the database in order to login and get access to the app.  So using the seed feature with Laravel is a big time saver for this.

So right out of the box there is file where information can be added to insert new information into the database by running a 'php artisan' command.

Open your app and find 'database\seeds\DatabaseSeeder.php'

public function run()
    {
        DB::table('users')->insert([
            'name' => 'Paul',
            'username' => 'admin',
            'email' => 'example@example.com',
            'password' => bcrypt('password'),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date)'Y-m-d H:i:s')
        ]);

    }

Run: php artisan db:seed

It is also possible to run the seed with a migration as well.
php artisan migrate --seed
php artisan migrate:refresh --seed
After running the seed command (assuming no errors) your tables will be populated with the user details or whatever you need.

If you need to add multiple rows of data you will need to send an array of arrays like so:

 DB::table('users')->insert([
            [
              'name' => 'Paul',
              'username' => 'admin',
              'email' => 'example@example.com',
              'password' => bcrypt('password'),
              'created_at' => date('Y-m-d H:i:s'),
              'updated_at' => date)'Y-m-d H:i:s')
             ],
             [
              'name' => 'Bob',
              'username' => 'bobby',
              'email' => 'bob@example.com',
              'password' => bcrypt('bobspassword'),
              'created_at' => date('Y-m-d H:i:s'),
              'updated_at' => date)'Y-m-d H:i:s')
             ]
        ]);

Note: how both the rows of data are their own array but they are wrapped within an array.

It is also possible to insert using the model instance e.g.

User::insert([
      [
        //Your code here
       ],
      [
        //Etc
       ]
]);


Don’t forget to add use App\User above the class at the top of the page or change User to App\User above.

Oh, one last thing.  When you use insert the updated_at and created_at fields will not be populated, therefor using date('Y-m-d H:i:s') will remedy this. It is also possible to use the User::create however you can only pass a single row.

If you need to add more data, check out factories. They make adding heaps of data a cinch.

Adding the Laravel Plugin to PhpStorm

The Laravel plugin assists with Blade templates including the new Laravel 5.4 update which includes slots and components.

Download the plugin for PhpStorm.

Go to Jetbrains and locate the plugin. https://plugins.jetbrains.com/idea/plugin/7532-laravel-plugin

Save to your computer somewhere and remeber where this is.  Desktop is easy.

Add the plugin to PhpStorm

Go to settings > plugins and click the button down the bottom. ‘Install plugin from disk…’

Locate the plugin Laravel.jar you have saved somewhere and select and install it.

Restart PhpStorm

Activate the plugin for your project.

Open the project you want to use the Laravel plugin for. Obviously this should be a Laravel project.

Settings > languages & frameworks > Php > Laravel

Check the ‘Enable plugin for this project’. Apply / OK.

And you are done.