Advertisement
Google Ad Slot: content-top
Laravel Faker
What is Faker?
Faker is a PHP library used in Laravel Factories to generate dummy test data such as names, emails, addresses, phone numbers, dates, etc.
It helps developers seed databases with fake but realistic data for testing and prototyping.
In Laravel, Faker is automatically available inside Factories as $this->faker.
Usage in Factory
Example factory using Faker:
class UserFactory extends Factory
{
public function definition(): array
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'phone' => $this->faker->phoneNumber,
'dob' => $this->faker->date,
'address' => $this->faker->address,
];
}
}
Faker Data Types
Here’s the list of commonly used $this->faker methods (grouped by category) with examples:
1️⃣ Person Data
|
Method |
Example Output |
|---|---|
|
$this->faker->name |
"John Doe" |
|
$this->faker->firstName |
"Michael" |
|
$this->faker->lastName |
"Smith" |
|
$this->faker->title |
"Mr." |
|
$this->faker->titleMale |
"Mr." |
|
$this->faker->titleFemale |
"Mrs." |
2️⃣ Address
|
Method |
Example Output |
|---|---|
|
$this->faker->address |
"123 Main St, New York, NY 10001" |
|
$this->faker->streetName |
"Maple Street" |
|
$this->faker->streetAddress |
"742 Evergreen Terrace" |
|
$this->faker->city |
"Los Angeles" |
|
$this->faker->state |
"California" |
|
$this->faker->postcode |
"90210" |
|
$this->faker->country |
"India" |
|
$this->faker->latitude |
"37.7749" |
|
$this->faker->longitude |
"-122.4194" |
3️⃣ Phone / Email / Internet
|
Method |
Example Output |
|---|---|
|
$this->faker->phoneNumber |
"+91 9876543210" |
|
$this->faker->email |
|
|
$this->faker->unique()->safeEmail |
|
|
$this->faker->userName |
"cool_guy99" |
|
$this->faker->domainName |
"example.org" |
|
$this->faker->url |
|
|
$this->faker->ipv4 |
"192.168.1.1" |
|
$this->faker->ipv6 |
"2001:db8::1" |
4️⃣ Text / String
|
Method |
Example Output |
|---|---|
|
$this->faker->word |
"developer" |
|
$this->faker->sentence |
"Laravel makes development simple." |
|
$this->faker->paragraph |
"This is a longer piece of text used for testing..." |
|
$this->faker->text(100) |
Random 100-character string |
5️⃣ Date & Time
|
Method |
Example Output |
|---|---|
|
$this->faker->date |
"2025-08-22" |
|
$this->faker->time |
"14:30:45" |
|
$this->faker->dateTime |
"2025-08-22 18:15:00" |
|
$this->faker->dateTimeBetween('-1 years', 'now') |
Random date in past year |
|
$this->faker->dateTimeThisYear |
Random date in current year |
|
$this->faker->dayOfWeek |
"Monday" |
|
$this->faker->monthName |
"August" |
6️⃣ Numbers
|
Method |
Example Output |
|---|---|
|
$this->faker->randomDigit |
7 |
|
$this->faker->randomDigitNotNull |
5 |
|
$this->faker->numberBetween(100, 200) |
145 |
|
$this->faker->randomNumber(5) |
48392 |
|
$this->faker->randomFloat(2, 1, 100) |
45.67 |
7️⃣ Commerce / Payment
|
Method |
Example Output |
|---|---|
|
$this->faker->company |
"TechSoft Pvt Ltd" |
|
$this->faker->jobTitle |
"Software Engineer" |
|
$this->faker->creditCardNumber |
"4532 8473 9123 4567" |
|
$this->faker->creditCardExpirationDate |
"08/27" |
|
$this->faker->iban |
"DE89370400440532013000" |
|
$this->faker->swiftBicNumber |
"DEUTDEFF" |
8️⃣ Images & Files
|
Method |
Example Output |
|---|---|
|
$this->faker->imageUrl(640, 480, 'cats') |
|
|
$this->faker->image('public/storage/images', 400, 300, null, false) |
"image1.jpg" |
|
$this->faker->file('/tmp', '/storage/app/uploads') |
Path to copied file |
9️⃣ UUID & Slug
|
Method |
Example Output |
|---|---|
|
$this->faker->uuid |
"550e8400-e29b-41d4-a716-446655440000" |
|
$this->faker->slug |
"laravel-query-builder" |
🔟 Custom Generators
You can combine Faker methods:
$this->faker->unique()->randomElement(['gold', 'silver', 'bronze']); $this->faker->boolean; // true / false $this->faker->optional()->word; // may return null