Getting Started

Strategies

Strategies allows to play with faker during random data generation

Strategies are a way to control the data you are generating, you are free to add as many as you want

Unique

Make sure the data you generate is not duplicated with this strategy

use Xefi\Faker\Faker;

$faker = new Faker();

$faker->unique()->numberBetween(1, 2); // 1
$faker->unique()->numberBetween(1, 2); // 2
$faker->unique()->numberBetween(1, 2); // Error too many tries

The unique strategy also gives the possibility to use a seed to have multiple unique generator:

use Xefi\Faker\Faker;

$faker = new Faker();

$faker->unique('my-seed')->numberBetween(1, 2); // 1
$faker->unique('my-seed')->numberBetween(1, 2); // 2
$faker->unique('my-other-seed')->numberBetween(1, 2); // 1

Regex

Regex strategy gives you the possibility to validate your data with a given regex expression:

use Xefi\Faker\Faker;

$faker = new Faker();

$faker->regex('/^01$/')->postalCode(); // Postal code that begins with '01'
$faker->regex('/.com$/')->email(); // Email that ends with '.com'

Valid

The valid strategy allows you to filter generated values using a custom validation callable. The callable must accept one parameter (the generated value) and return a boolean indicating whether the value is valid or not.

use Xefi\Faker\Faker;

$faker = new Faker();

$faker->valid(function ($number) {return $number % 2 === 0; })->numberBetween(1, 100); // Even numbers
$faker->valid(fn ($number) => $number % 2 !== 0)->numberBetween(1, 100); // Odd numbers
$faker->valid(fn ($email) => str_ends_with($email, '.org'))->email(); // Emails that ends by '.org'

Copyright © 2026