Better Know a Library: Faker
When you're developing websites it's very easy to create a small subset of data so you can test but there are some performance issues and bugs that will only rear their ugly head when you have a large amount of data. It's very difficult to create this test data and have unique values set. You could just fill your database with random letters and numbers (substr(sha1(gmdate('U')))
, 0, 10) but it's better to have nice looking data if you need to demo changes.
The Faker library provides a way to generate fake but real looking data for your test environment.
Installation
Installation is done via composer:
./composer.phar require fzaninotto/Faker
Basic Usage
<?php
// require the Faker autoloader
require_once('vendor/fzaninotto/faker/src/autoload.php');
// initialize the library
$faker = Faker\Factory::create();
// output a full name
echo $faker->name, PHP_EOL;
// just first name
echo $faker->firstName, PHP_EOL;
// just last name
echo $faker->lastName, PHP_EOL;
Results:
$ php test.php
Herta Abernathy
Hudson
Frami
The cool thing is that because it's random data we can run the script over and over again and get different results:
$ php test.php
Miss Karlee Shanahan
Billy
Haley
$ php test.php
Mr. Guiseppe Fay Jr.
Elaina
Herman
$
We can also seed the random number generator so we get the same results every time.
// initialize the library
$faker = Faker\Factory::create();
$faker->seed(1000); // this number picked randomly
Results:
$ php test.php
Prof. Brian Monahan
Willis
Lebsack
$ php test.php
Prof. Brian Monahan
Willis
Lebsack
A full list of the fields you can use can be found in the formatters section of the Faker documentation. Some of the ones I use often are:
$faker->text; // returns a large number amount of text
$faker->sentence; // returns a sentence or two
$faker->address;
$faker->stateAbbr;
$faker->city;
$faker->postcode;
$faker->phoneNumber
The real power of this library comes when you need to fill your database with data:
for ($i = 0; $i< 10000; $i++) {
$faker->seed($i); // this causes the same results even if you add additional fields/related objects
$user = new User();
$user
->setFirstName($faker->firstName)
->setLastName($faker->lastName)
->setEmail($faker->safeEmail);
}
I usually create a command line script or a model that creates this fake data so it's easily added to a newly created devel machine.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows