Add Faker to Laravel for Dummy Data

When it comes to generating dummy data for PHP application, Faker is the most well-known library. It can be used to generate any types of data, from a number, human’s name, city, to credit card number, username, password.

I wrote a post about using Faker in CodeIgniter. There are some steps to follow to use Faker. However, we don’t need those steps applied to Laravel, the framework (v5.0+) already has Faker built-in.

To test some functions of the Faker library you just need to create a Faker object and return data from some methods from your web routes file .

Add library

use Faker\Factory as Faker;

Test function in web route

Route::get('/dummy',function(){
	$faker = Faker::create();
	echo 'Email Address: ' . $faker->unique()->email;
});

Insert data

$faker = Faker::create();
DB::table('account')->insert([
	'username' => $faker->userName(),
	'password' => $faker->password(),
	'about' => $faker->sentence(),	
	'created_at' => \Carbon\Carbon::now(),
	'Updated_at' => \Carbon\Carbon::now(),
]);

Leave a Comment

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

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close