Generate Dummy Data with Faker in Laravel

Starting from Laravel 5, Faker is included by default in the framework. The Faker library is a great tool for generating dummy data. It’s been around for quite a while and is used by a lot of developers. One of the benefits of using the Faker library is that you can generate realistic data. This can be helpful for testing purposes or when you need to populate a database with data.

In this tutorial, we’ll show you how to use the Faker library to generate dummy data in a Laravel application.

use Faker\Factory as Faker;

$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(),
]);
//it can be used in route
Route::get('/dummy',function(){
    $faker = Faker::create();
    echo 'Random Address: ' . $faker->unique()->email;
});

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