9951 explained code solutions for 126 technologies


php-fakerHow can I use Faker with Laravel?


Faker is a PHP library that generates fake data for you to use in your projects. It can be used with Laravel in order to generate dummy data for testing purposes. Here is an example of how to use Faker with Laravel:

<?php

use Faker\Factory as Faker;

$faker = Faker::create();

foreach (range(1, 10) as $index) {
    echo $faker->name . "\n";
}

// Output:
// Dr. Zane Koch
// Mrs. Joanne Schulist
// Dr. Priscilla Kihn
// Ms. Myrtis Schoen
// Mrs. Zella Klocko
// Miss Marjorie Hermann
// Mrs. Louvenia Kassulke
// Mr. Kaleb Jacobs
// Mr. Albin Kirlin
// Ms. Lilliana O'Keefe

In this example, we use the Faker::create() method to create a new instance of the Faker class. Then, we use a foreach loop to generate 10 names using the name method. Finally, we print out the names to the screen.

Code explanation

  1. use Faker\Factory as Faker; - This imports the Faker library.
  2. $faker = Faker::create(); - This creates a new instance of the Faker class.
  3. foreach (range(1, 10) as $index) { - This loop iterates 10 times.
  4. echo $faker->name . "\n"; - This generates and prints out a fake name.

Helpful links

Edit this code on GitHub