Laravel Faker generate dummy data for testing

Laravel Faker generate dummy data for testing

  • 14 July, 2018
  • Renish Khunt
  • Laravel
  • 116

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.

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.