Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

390
Views
How to generate a unique fake email with a custom domain with faker?

I have a laravel application that requires the registered users must use their company email (custom domain).

So how i can i achieve that with faker generators to test it with my model factories ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use a simple trick with php's preg_replace function:

preg_replace('/@example\..*/', '@domain.com', $faker->unique()->safeEmail)

so your laravel model factory might looks like this:

$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;
    return [
        'name' => $faker->name,
        'email' => preg_replace('/@example\..*/', '@domain.com', $faker->unique()->safeEmail),
        'password' => $password ?: $password = bcrypt('secret'),
        'avatar' => $faker->imageUrl,
        'remember_token' => str_random(10),
    ];
});
over 4 years ago · Santiago Trujillo Report

0

You can use the Faker\Generator to create an auxiliary variables:

$factory->define(App\User::class, function (Faker\Generator $faker) {
    $userName = $faker->userName;
    $uniqueSuffix = $faker->unique()->word;
    $domain = 'your.domain.com';
    $uniqueFakeEmail = "$userName.$uniqueSuffix@$domain";

    return [
        'email' => $uniqueFakeEmail,
        // ...
    ];
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!