skip to Main Content

Laravel Faker Generate Dummy Data For Testing

Laravel Faker generate dummy data for testing


Nowadays, The Laravel is the most popular framework. The most of the project is developed on the Laravel. On the many projects, we need to test project with many dummy data. If we create the testing data then it will take too much time. So let’s see, how we can create the much more data without westing the time.

We can able to create the thousands of testing data without wasting time. Yes, we can do using Laravel Faker module. Let’s see, How we can install faker module on the Laravel and generate the thousand of testing data for your enterprise level application.

First of all, we going to installed Faker package

composer require fzaninotto/faker

Fire this command at the root of Laravel.

Wait for some time till installing the Faker module. After installing Faker module Let’s create the dummy users with faker/dummy data.

$faker = \Faker\Factory::create();
$data = [];
for ($i=0; $i < 2000; $i++) { 
    $data[] = [
        'name' => $faker->name,
        'email' => $faker->safeEmail,
        'password' => bcrypt('thecodingstuff'),
        'first_name' => $faker->firstName,
        'last_name' => $faker->lastName,
        'mobile' => $faker->phoneNumber,
        'country' => $faker->country,
        'state' => $faker->state,
        'city' => $faker->city,
        'pincode' => $faker->postcode,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
    ];
}
// Insert the testing user to users table.
User::insert($data);

Just change the number of for loop and generate the thousands of users.

You can generate any kind of the data. for more detail visit Faker repository.
If you any question write the comment below.

Saroj

I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

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

Back To Top