9951 explained code solutions for 126 technologies


php-fakerHow can I generate unique data with Laravel Faker?


You can generate unique data with Laravel Faker by using the unique() method. This method will generate a random unique value each time it is called.

For example:

$uniqueNumber = \Faker\Factory::create()->unique()->numberBetween($min = 1, $max = 9999);
echo $uniqueNumber;

Output example

5577

The code above will generate a random number between 1 and 9999.

The parts of the code are:

  • \Faker\Factory::create() - creates a new Faker\Generator instance
  • unique() - the method used to generate a unique value
  • numberBetween($min = 1, $max = 9999) - generates a random number between 1 and 9999

For more information about Laravel Faker, please refer to the Laravel Faker Documentation.

Edit this code on GitHub