How to Seed Database Using PHP Faker Library

You have developed an application. Now, to test your application you need fake data into the database. And you don’t want to fill it up manually. Filling data manually in the database is a time-consuming process. As a developer, we should not do this tedious task. We are developers and we should always prefer automated tools to perform such tasks.

So the question arises is there any fake data generator tool available that can seed tables in the database?

Faker is an open-source library for PHP applications. It generates fake data which you can insert into the database.

Using the Faker library one can insert tons of dummy data into the database in a short time. All you need to do is generate dummy data like fake email, fake name, etc through Faker Formatters. 

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

Getting Started

A recommended way to install the 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 fakerphp/faker

Next, as we need to seed the database with fake entries, create a table called users. In this table, I am taking columns in such a way that we can utilize different Formatters.

  • fullname: name() formatter will be used.
  • email: email() formatter.
  • identification_number: Use randomNumber() which generates random number. I’ll create a random number of 9 digits.
  • bio: paragraph() formatter gives 3 sentences by default.

After seeding the users table, it will display something like the below screenshot.

seed-database

Write a Code to Seed Database

I assume you are using core PHP and I am writing a code for database connection and insertion query accordingly. If you are using any Framework or CMS then your code will be a little bit different but the logic will be the same.

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

<?php
require_once "vendor/autoload.php";
 
$conn = mysqli_connect("DB_HOST", "DB_USERNAME", "DB_PASSWORD", "DB_NAME");
 
$faker = Faker\Factory::create();
 
for ($i=0; $i < 100; $i++) {
    $sql = "INSERT INTO users(fullname, email, identification_number, bio) VALUES('" . $faker->name() . "', '" . $faker->email() . "', '" . $faker->randomNumber(9, true) . "', '" . $faker->paragraph() . "')";
    mysqli_query($conn, $sql);
}

echo "Fake entries inserted successfully.";

Make sure to replace placeholders with your actual database information.

The above code will insert 100 fake entries in the users table. You can change this limit as per your requirement. In the code, I used a few Faker formatters to generate fake data.

How Faker Formatters Work?

On the official documentation, under Formatters you will see a list of providers.

Let’s take an example of a sentence() provider. This provider gives us a sentence containing a few words. It can be used as follows.

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

However, we can generate a sentence with a given amount of words. If we need a sentence with 20 words, then pass 20 as a parameter to the method.

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

Now you will get a fake sentence containing 20 words.

This is how Faker’s formatters work. I’d recommend learning other formatters and using them as per your table structure.

I hope you understand how to seed the database using the Faker library in PHP. I 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 *