Generate Dummy Data in CodeIgniter with Faker Library

Faker is a popular PHP library that can be used in any PHP framework like CodeIgniter to generate dummy data for development or testing purposes. Faker can be used to bootstrap database and XML documents, and fill in your persistence to stress test it.

Faker allows dummy data generation for Lorem Ipsum Text, Person, Address, Phone Number, Company, Real Text, Date and Time, Internet, User Agent, Payment, Color, File, Image, Uuid, Barcode, Miscellaneous, Biased, and Html Lorem. It supports both PSR-0 and PSR-4 autoloaders.

Note: Faker’s repository has been archived by the owner. It won’t affect developers who want to use this library. The owner’s decision to archive the project is to avoid users abusing the database and add records that are critical to real-life business.

Steps to add Faker to CodeIgniter using PSR-0:

  • Download the library from https://github.com/fzaninotto/Faker.
  • Copy it to application\third_party\, something like application\third_party\faker of your CodeIgniter project folder.
  • The library than can be added to any controllers that require it with include APPPATH . 'third_party/faker/autoload.php';

Create an object of the Faker class with $faker = Faker\Factory::create(); then use it to generate data with respective methods.

// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();
//some examples
echo $faker->name;
echo $faker->address;
echo $faker->text;
echo $faker->numberBetween(10, 1000);
echo $faker->email;
echo $faker->hexcolor();

//user
$user = [
    'name' => $faker->name,
    'email' => $faker->unique()->email,
    'password' => password_hash($faker->password, PASSWORD_DEFAULT),
    'phone' => $faker->phoneNumber,
    'address' => [
        'street' => $faker->streetName,
        'number' => $faker->buildingNumber,
        'city' => $faker->city,
        'state' => $faker->stateAbbr,
        'country' => $faker->country,
        'postcode' => $faker->postcode,
    ],
    'date_of_birth' => $faker->dateTimeBetween('-60 years', '-18 years')->format('Y-m-d'),
];

echo json_encode($user, JSON_PRETTY_PRINT);

//product
$product = [
    'name' => $faker->words(3, true),
    'description' => $faker->paragraphs(3, true),
    'price' => $faker->randomFloat(2, 10, 100),
    'quantity' => $faker->numberBetween(1, 100),
    'sku' => $faker->unique()->regexify('[A-Z]{3}-\d{3}-[A-Z]{2}'),
    'category' => $faker->randomElement(['Electronics', 'Clothing', 'Home', 'Sports']),
    'images' => $faker->randomElements(['https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg'], $count = $faker->numberBetween(1, 3)),
];

//Latitude and Longitude
$latitude = $faker->latitude($min = -90, $max = 90);
$longitude = $faker->longitude($min = -180, $max = 180);

echo "Latitude: $latitude\n";
echo "Longitude: $longitude\n";

Leave a Comment

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


Scroll to Top

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