How To Seed Database Using PHP Faker Library

You have developed an application. You have created a database for your website. Now, to test your application you need some kind of fake data. Obviously, the question arises is there any fake data generator tool available online which can seed tables in the database?

Faker is a PHP library that generates fake data for your application.

Filling data manually in the database is a time-consuming process. As a developer, we should not do this process manually. We are a developer and quite forward so we should always prefer automated tools to perform our tasks.

Using Faker library we can insert tons of dummy data into our database in few seconds. All we need to do is generate dummy data like fake email, fake name, etc through Faker Formatters.

Having said that, let’s see step by step guide on using Faker library.

Installation

Recommended way to install Faker library is through Composer. If you have not installed Composer on your computer then you can download it from here.

Open the terminal in your project root directory and run the below command:

composer require fzaninotto/faker

Next, we need to generate a dummy data for our database table. Let’s say we have a table called ‘users’ and it contains columns like fullname, email, password, created_at. And we need to insert fake data in the ‘users’ table.

Seed Table

Code For Seed Database

As we need to insert fake data into our database, we need to first connect our database.

For this article, we assume you are using core PHP so we write a code for database connection and queries accordingly. If you are using any Framework or CMS then your code will little bit different but logic would be same.

Create seed.php file into your project root directory and place the below code in it.

<?php
require_once "vendor/autoload.php";

$conn = mysqli_connect("localhost", "root", "", "my_database");

$faker = Faker\Factory::create();

for ($i=0; $i < 100; $i++) { 
    $sql = "INSERT INTO users (fullname, email, password, created_at) VALUES ('" . $faker->name . "', '" . $faker->email . "', '" . md5($faker->password) . "', '" . date('Y-m-d H:i:s', strtotime($faker->iso8601)) . "' )";
    mysqli_query($conn, $sql);
}
?>

Above code will insert 100 fake entries in the ‘users’ table of the database ‘my_database’.

In our code, we used the Faker providers like name, email, password and iso8601. There is a number of other providers we can use in Faker library.

How Faker Providers Works?

On the github page of Faker library, under Formatters you will see list of providers.

Let’s take an example of Faker\Provider\Lorem provider. This provider has some formatters like a word, paragraphs, etc as shown in below screenshot.

Lorem Provider

As an example, we take one scenario where we need to generate a dummy sentence. So using faker, we do it as follows.

<?php
$faker = Faker\Factory::create();
echo $faker->sentence;
?>

It gives us sentence which contains 6 words. This is because the method sentence has the first parameter with a default value of 6. If we need a sentence with 20 words then our code will as follows.

<?php
$faker = Faker\Factory::create();
echo $faker->sentence(20);
?>

Above code will give a sentence with 20 words.

This is how formatters works in Faker library. You can use other formatters as per your requirements or as per your table structures.

We hope you understand how to seed database using Faker library in PHP. We would like to hear your thoughts in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *