Chose Language:
Author: Admin/Publisher |finished | checked

PHP faker package

In some projects, you may need to populate your database with information. For this purpose, you can use the PHP Faker package, which you can find on the main Composer Packagist repository.

These days, I believe it’s almost necessary to use Composer in your project. You can learn more about Composer in this other post: https://blastcoding.com/que-es-composer/

Keep in mind that we’ll also be using Composer’s autoloading.

Para utlizar faker necesitaras PHP 7.4 o superior

¿Que es faker?

The FakerPHP library allows us to generate fake data for testing purposes. This data helps us understand how our system’s structure works.

Keep in mind that we’ve created a system, and we can’t test it with real client data. The data we use for testing must be fake. Manually entering test data is not practical, as it would be time-consuming.

The smart approach is to use a library like FakerPHP to automatically generate this data for us.

Please note that there may be other libraries or packages available to accomplish this task. Here, we are using FakerPHP as an example. Feel free to use the one that suits your needs best.

Installing Faker

Faker is easy to install. Let’s create a project and initialize Composer in it. Once we have Composer in our project, we can add Faker to it as follows:

Project Folder
composer require fakerphp/faker

Using the PHP faker package (fakerPHP)

In Faker, there’s a really simple example. So, let’s first try Faker with this example. Create an index.php file inside your project folder and add these lines of code:

index.php
require_once 'vendor/autoload.php';

// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();
// generate data by calling methods
echo $faker->name();
// 'Vince Sporer'
echo $faker->email();
// 'walter.sophia@hotmail.com'
echo $faker->text();
// 'Numquam ut mollitia at consequuntur inventore dolorem.'
Dr. Niko Maggio Sr.frieda39@yahoo.comRem dolores perspiciatis iure sunt. In dolor dolores nostrum totam. Quos dolorum rerum asperiores.

Alright, the library is working. Let’s create the script we used for the PHP pagination exercises, which created the “users” table and filled it with fake users. This time, we’ll create the following index.php:

index.php
require_once 'vendor/autoload.php';

// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();


$quantity = 150;

$create_table= "CREATE TABLE usuarios (
id INT AUTO_INCREMENT PRIMARY KEY,
nombre VARCHAR(50),
apellido VARCHAR(50),
email VARCHAR(100)
);"; $query = "
INSERT INTO usuarios (nombre, apellido, email) VALUES
"; $values = ""; for ($i=0; $i < $quantity; $i++) { $values .= "(\"".$faker->name()."\",\"".$faker->lastName()."\",\"".$faker->email()."\"),
"; if (($i+1) == $quantity) { $values .= "(\"".$faker->name()."\",\"".$faker->lastName()."\",\"".$faker->email()."\");"; } } echo $create_table.$query.$values;

I see that we need to create the table and use Faker within the insert statement to create the values. We’ll use a loop to create 150 users, although it looks like there’s a mistake in the code because I see 151 users being created.

Faker formatters

As you can see in the examples, we’ve used $faker->name(), $faker->lastName(), $faker->email(), and $faker->text(). These methods are called formatters.

Faker provides an incredible range of formatters, which you can check on the official Faker page at https://fakerphp.github.io/formatters/.

Formatters can range from a user’s name to a credit card number or even barcode codes.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category