
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
Many times for the sake of testing you might have dumped manually form fields fake data to the database. Lot of times while demoing the applications to your client you may have seen this fake junk data from your database which may not make any sense to your clients, even they might not understand lot of things for this purpose.
In this article, you will learn how to generate the fake data with PHP Faker from this package fzaninotto/Faker.
I hope you have composer
installed in your system. Composer is a PHP dependency manager. To install composer you can use the following command
Linux/Ubunut
sudo apt-get update
sudo apt-get install composer
I am creating new project test
inside htdocs
folder, you might have to create inside /var/www/html
or any other working directory for your PHP.
First and the foremost thing is PHP Faker library you can install it with the help of the following command. Go to your htdocs/test
project and run the following
composer require fzaninotto/faker
After installation you will have the following project folder structure. After using the above command you will have
For the sake of demonstration I will use fresh plain PHP project so that you will know how to implement in any other PHP frameworks too.
As shown in the above image I will write everything in index.php
file.
<?php
require_once './vendor/autoload.php';
Don't forget to add autoload.php
file which is inside vendor/autoload.php
file.
(Default en_US)
Now you can generate the fake data in the following way I have included comments to make sure you understand those
<?php
/** Include autoload.php for autoloading the classes used in Faker */
require_once './vendor/autoload.php';
/** Faker Factory Object */
$faker = Faker\Factory::create();
/** By default it uses en_US names, I will show you how to change this */
/** Full Name Generation */
echo $faker->name."<br>";
/** First Name & Last Name Generation */
echo $faker->firstNameFemale.' '.$faker->lastName."<br>";
/** Female Title like Mrs. Miss. */
/** You can also use $fakerTitleFemale */
echo $faker->title('female')."<br>";
/** Male Title like Mr. Mrs. */
/** You can also use $fakerTitleMale */
echo $faker->title('male')."<br>";
/** Jr., II, DDS Make sure this exists in Faker Factory Provider, For Indian locale it wont work */
echo $faker->suffix."<br>";
As we saw we know we can see that we are able to see the above details shows some English names, we do have an option to set the different country locale. List of different country options is available in the following
To set the locale use the following code. Here I am using en_IN
as the default country data to populate
<?php
/** Include autoload.php for autoloading the classes used in Faker */
require_once './vendor/autoload.php';
/** Faker Factory Object */
$faker = Faker\Factory::create('en_IN');
/** Full Name Generation */
echo $faker->name."<br>";
/** Rest of the above code remains same other than suffix, as suffix wont exist in india */
As you saw above that you can generate names, if you want to generate for example 50 names then you can use the loop like the following
<?php
/** Include autoload.php for autoloading the classes used in Faker */
require_once './vendor/autoload.php';
/** Faker Factory Object */
$faker = Faker\Factory::create('en_IN');
for($i = 0; $i <= 50; $i++){
echo $faker->name."<br>";
/** You can use DB Connection To Store Data */
}
You can generate list of other data like LoremIpsum, Address, Phone, Company, Text, DateTime, Internet, User Agent, Payment and many more.
For more details on the same & how to use those you can use the following GITHUB link for the repo Faker Library
You learnt how to generate the fake data for faster data adding to your database. You might also be interested to learn few more topics I have written
Factory States For Clean And Fluent Laravel Testing
Setup Docker for NodeJs, MongoDB, MongoDB Compass
Use Different PHP Versions In Ubuntu / Linux
Generate Fake Data In PHP With Faker
Laravel Last Executed Query In Plain SQL Statement For Debugging
Multiple GIT Key Pairs Or Account In The Same Computer
Create Custom 404 Page In Laravel
Generate RSS Feeds in PHP Laravel
Client-Side DataTable, Adding Super Powers To HTML Table
Install NGINX In Linux / Ubuntu And Managing
Comment And Like System Using Disqus
NGINX Security Best Practices & Optimization
Global Data In All Laravel Blade Pages
PHP file_put_contents failed to open stream: Permission denied