😁$this->faker is an instance of the FakerPHP library (ok)

In Laravel, $this->faker is an instance of the FakerPHP libraryarrow-up-right available within Model Factoriesarrow-up-right and tests using the WithFaker trait. LaravelLaravel +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 docsFaker 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 docsFaker 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 coordinates Faker docsFaker 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 address LaravelLaravel +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(): true or false

6. ModifiersFaker allows you to wrap calls to ensure specific behaviors: PackagistPackagist

  • $this->faker->unique()->email(): Ensures the generated email is unique for that run.

  • $this->faker->optional()->text(): Sometimes returns the value, sometimes returns null.

  • $this->faker->randomElement(['active', 'pending', 'deleted']): Picks one item from a custom array. LaracastsLaracasts +2

For a complete and searchable list of every available formatter, refer to the FakerPHP Available Formatters Documentationarrow-up-right.

Last updated