😁$this->faker is an instance of the FakerPHP library (ok)
In Laravel, $this->faker is an instance of the FakerPHP library available within Model Factories and tests using the WithFaker trait. Laravel +1Below is a categorized list of commonly used methods (formatters) available via
$this->faker:1. Personal Information
$this->faker->name(): Full name$this->faker->firstName()/lastName(): Specific parts of a name$this->faker->title(): Prefix (e.g., Mr., Mrs., Dr.)$this->faker->phoneNumber(): Random phone number$this->faker->jobTitle(): Job title (e.g., Developer)Faker docs +3
2. Text & Content
$this->faker->word(): A single random word$this->faker->sentence(): A single sentence$this->faker->paragraph(): A single paragraph$this->faker->text(): A larger block of text$this->faker->realText(): More natural-sounding text (available for different locales)Faker docs +4
3. Address & Location
$this->faker->address(): Full address$this->faker->streetAddress(): Street name and number$this->faker->city()/state()/postcode(): Specific location parts$this->faker->country(): Random country name$this->faker->latitude()/longitude(): Geographic coordinatesFaker docs +2
4. Internet & Tech
$this->faker->safeEmail(): An email address guaranteed not to exist$this->faker->userName(): A random username$this->faker->password(): A random string for passwords$this->faker->domainName(): Example:example.com$this->faker->url(): A full random URL$this->faker->ipv4(): A random IP addressLaravel +2
5. Dates & Numbers
$this->faker->date(): Random date (e.g.,2024-03-25)$this->faker->dateTimeBetween('-1 year', 'now'): A date object within a range$this->faker->randomNumber(): A random integer$this->faker->randomFloat(): A random decimal number$this->faker->boolean():trueorfalse
6. ModifiersFaker allows you to wrap calls to ensure specific behaviors: Packagist
$this->faker->unique()->email(): Ensures the generated email is unique for that run.$this->faker->optional()->text(): Sometimes returns the value, sometimes returnsnull.$this->faker->randomElement(['active', 'pending', 'deleted']): Picks one item from a custom array.Laracasts +2
For a complete and searchable list of every available formatter, refer to the FakerPHP Available Formatters Documentation.
Last updated