Laravel incorporates the faker library to work with test data in our database, in this article we will see how to use this resource
Laravel Faker
In its latest versions, Laravel already comes with this library, previously to install we had to do it as follows with composer
composer require fzaninotto/faker
Now when it comes with the integrated library, we can see the package in require-dev which would be the packages that are used in development mode and require in production mode
"require": {
"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.2",
"laravel/sanctum": "^2.14.1",
"laravel/tinker": "^2.7",
"stripe/stripe-php": "^7.121"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^6.1",
"phpunit/phpunit": "^9.5.10",
"spatie/laravel-ignition": "^1.0"
},
Formatters
The library has a large number of formatters to generate random data, some examples are the following
Faker\Provider\Base
randomDigit
randomLetter
Faker\Provider\Lorem
word
text($maxNbChars = 200)
Faker\Provider\en_US
titleMale
firstNameFemale
The library has a large number of methods, where we can generate any type of false data to test our application in a local environment, for a reason Laravel incorporates this package in its Laravel factory in its UserFactory.php file
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
Without a doubt, this library has become a great tool when creating php web applications, in laravel or any other framework such as codeigniter, cakephp, yii, symfony, laminas mvc