Quick Tip: Faker and Laravel Seed Generators

One of the best parts of Laravel, in my mind, anyway, are all of the workflow tools that allow you to get up and running quickly, and remove all the drudge tasks from your day. The Artisan console is one of the best things going in frameworks these days, and no one has helped increase its "awesomeness" than Jeffrey Way and his Laravel Generators

I wanted to share with you another great library I found recently called Faker by Francois Zaninotto, and how easy it is to combine it with the Data Seed Generator to save you hours of pesky work.

What we're focusing on is the very simple workflow found at the bottom of the generators tutorial (find: "workflow" to take you there on the page). In it, we are making a Dogs module & seeding it with some sample data. We want to use the Generator installation guide, but add Faker to our composer json, so it looks like:

"require": {
    "laravel/framework": "4.0.*",
    "way/generators": "dev-master",
        "fzaninotto/faker": "1.2.*@dev"

Then, for the sake of this post, I'll repeat the walkthrough instructions:

// from commandline
php artisan generate:resource dog --fields="name:string, age:integer"

//open file database/seeds/DogsTableSeeder.php and in the run() function add:
$dogs = [
    ['name' => 'Sparky', 'age' => 5],
    ['name' => 'Joe', 'age' => 11]

// commandline again
php artisan migrate
php artisan db:seed

//in your database admin tool you'll see 2 records have been added to a table named Dogs

Now, that's absolutely wonderful, but it still leaves us with one small issue - we have to type out an array for every dog we want to seed the table with. Not so fun. Certainly not what you, as a programmer, should be spending your time on.

Enter Faker

Faker is not the only random data generator out there, but I find it useful for its variety of language names & ease of extension. The instructions are quite clearly explained on the site's github page, with good examples and even a little under-the-hood explanation. I'd recommend giving it a look.

Since we have already loaded the files via composer, we can just start using it. Our goal is to autogenerate the $dogs array for our insert:

//remove this array
//$dogs = [
//    ['name' => 'Sparky', 'age' => 5],
//    ['name' => 'Joe', 'age' => 11]

//make a new Faker object. Let's use French names for dogs, so we'll also initialize
//with the FR data:
$faker = Faker\Factory::create('fr_FR');

//if we want to create the SAME seed generated data every time we run, instead of
//making file, just use the Faker->seed() call:

//now, make our data array:
for ($i=0; $i < 10; $i++) {
    $dog = array('name' => $faker->firstName, 'age' => rand(1, 12));
    $dogs[] = $dog;

//commandline again
php artisan db:seed

That's it! Look in your table, and you'll see 10 dogs, all with French names and ages between 1 and 12. Because we have labeled this seed, as many times as you run the db:seed command, you will always get the same set of data - no need to create a separate file.

Faker's data is focused on created user records, so you'll need to set up your Provider if you have other objects you'd like to use it with, but instructions are quite simple to follow. Hopefully as the popularity of this library grows, more and more providers will be available to work with - so do consider donating your own!

Hope that saved you some time!

Read it Monday, Use it by Friday!

Laravel Quick Tips Weekly Newsletter. Short, immediately helpful bits you'll use in your own codebase before the next one arrives.

Join us now and get a FREE PDF of the first fix months of Laravel Quick Tips!

No Spam, Unsubscribe Anytime

Contact me