Laravel — P31- Faker

Enhancing Laravel Data with Faker: Realistic Testing Made Easy

What’s a faker? It’s exactly as it sounds. You’re trying to fake something. Instead of coming up with the data yourself, you simply state, “Faker, give me a unique random email address,” and faker spits it out for you.

In Laravel, Faker is a library that generates fake data for testing purposes. It can be used to generate random names, addresses, phone numbers, email addresses, and other types of fake data that can be used to populate databases and test applications.

Laravel — P30: Factories

You can start using the faker library automatically. The Faker PHP library is pretty powerful and makes generating random names, emails, etc, incredibly easy. You can read the Faker documentation to find out more.

In our case, we just want a random name and a unique email. We can return the code in the UserFactory to its original state.

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'name' => fake()->name(),
            'email' => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     *
     * @return static
     */
    public function unverified()
    {
        return $this->state(fn (array $attributes) => [
            'email_verified_at' => null,
        ]);
    }
}

We can test to make sure that it’s working by running the same factory()->make() command in tinker. You can run this code as many times as you’d like and faker will generate new users each time.

# php artisan tinker
Psy Shell v0.11.9 (PHP 8.1.13 — cli) by Justin Hileman
> User::factory()->create();
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
= App\Models\User {#3828
    name: "Dr. Rosario Schinner MD",
    email: "elouise.howell@example.com",
    email_verified_at: "2023-01-15 23:08:56",
    #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
    #remember_token: "ZUO9ossy7p",
    updated_at: "2023-01-15 23:08:56",
    created_at: "2023-01-15 23:08:56",
    id: 5,
  }

> 

If you’re not getting the results that you were expecting, and currently have tinker open, you’ll need to close it (CTRL + C) and re-open it. You can now use your factory with faker to generate random users whenever you need them just like before.

What if you wanted to generate 50 users? Laravel makes this painless as well. Let’s look at it in tinker again.

> User::factory()->count(50)->create();
= Illuminate\Database\Eloquent\Collection {#3798
    all: [
      App\Models\User {#3836
        name: "Prof. Misael Hackett Sr.",
        email: "twilliamson@example.net",
        email_verified_at: "2023-01-15 23:11:00",
        #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
        #remember_token: "ENxk4Hsf7P",
        updated_at: "2023-01-15 23:11:00",
        created_at: "2023-01-15 23:11:00",
        id: 6,
      },
      App\Models\User {#3835
        name: "Prof. Iva Cronin",
        email: "koelpin.camron@example.org",
        email_verified_at: "2023-01-15 23:11:00",
        #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
        #remember_token: "uKchY1uPQZ",
        updated_at: "2023-01-15 23:11:00",
        created_at: "2023-01-15 23:11:00",
        id: 7,
      },
      App\Models\User {#3834
        name: "Trevion McDermott DDS",
        email: "friesen.austyn@example.org",
        email_verified_at: "2023-01-15 23:11:00",
        #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
        #remember_token: "v6pwNwJPym",
        updated_at: "2023-01-15 23:11:00",
        created_at: "2023-01-15 23:11:00",
        id: 8,
      },
      App\Models\User {#4571
        name: "Joel Baumbach",
        email: "okon.alison@example.com",
        email_verified_at: "2023-01-15 23:11:00",
        #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
        #remember_token: "PMA6L8DEke",
        updated_at: "2023-01-15 23:11:00",
        created_at: "2023-01-15 23:11:00",
        id: 9,
      },
      ...

And just like that, you have 50 new users with the help of the count() method that’s been chained on. If you don’t want to persist the data, you can use the make() method.

Check the users table in your database to verify that you have 50 new users.

mysql> select * from users;
+----+--------------------------+--------------------------------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
| id | name                     | email                          | email_verified_at   | password                                                     | remember_token | created_at          | updated_at          |
+----+--------------------------+--------------------------------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
|  1 | Dino Cajic               | dino@example.com               | 2023-01-14 17:10:19 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | ONtAKjJsOP     | 2023-01-14 17:10:19 | 2023-01-14 17:10:19 |
|  5 | Dr. Rosario Schinner MD  | elouise.howell@example.com     | 2023-01-15 23:08:56 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | ZUO9ossy7p     | 2023-01-15 23:08:56 | 2023-01-15 23:08:56 |
|  6 | Prof. Misael Hackett Sr. | twilliamson@example.net        | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | ENxk4Hsf7P     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
|  7 | Prof. Iva Cronin         | koelpin.camron@example.org     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | uKchY1uPQZ     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
|  8 | Trevion McDermott DDS    | friesen.austyn@example.org     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | v6pwNwJPym     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
|  9 | Joel Baumbach            | okon.alison@example.com        | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | PMA6L8DEke     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 10 | Marquise McGlynn         | berge.wyatt@example.com        | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | Lfi8MeDgYo     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 11 | Dr. Keagan Lynch DDS     | shanahan.aaliyah@example.net   | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | TQPOGQh8mk     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 12 | Aniya Heller             | nash.schmidt@example.org       | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | HghOC41HyG     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 13 | Scotty Shields           | kylee38@example.net            | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | fqB23DLDkJ     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 14 | Tyrique Denesik          | krajcik.merle@example.org      | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | M2CjqCn3qa     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 15 | Dr. Nathen Hyatt DDS     | simonis.lillie@example.org     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | p6l2HtVgQY     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 16 | Julian Gulgowski         | reichel.nikko@example.com      | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 7n7aiBrDGF     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 17 | Prof. Houston Daniel II  | stone85@example.net            | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | KxCH5zAK4W     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 18 | Mr. Keon Bayer           | bashirian.forrest@example.net  | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | lITXsHkjTm     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 19 | Mathias Turcotte         | uwilkinson@example.com         | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | DP1ZhswiSe     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 20 | Victoria Willms          | cayla00@example.net            | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | gctVGjMGok     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 21 | Prof. Tyler Donnelly     | delphia36@example.org          | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | vzaJFpVeoZ     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 22 | Connie Bruen             | lurline.heathcote@example.com  | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 0p1uBi7HZd     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 23 | Mr. Richmond Kirlin DDS  | gregory.spinka@example.net     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | JkXo96CBZX     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 24 | Gladys Cummerata         | garth14@example.net            | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | UoAxfKAaPO     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 25 | Miss Dariana Gleason     | lemke.adela@example.org        | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 9GSpjl91aM     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 26 | Jazlyn Turner II         | amir.ward@example.com          | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | tT0H7758bw     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 27 | Blanca Ondricka          | adolfo21@example.com           | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | oI6asdE8zB     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 28 | Isaias Hudson Jr.        | addison28@example.net          | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 1vadeQJDdj     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 29 | Cory Russel              | rbradtke@example.com           | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | OkDLHvU3cb     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 30 | Maynard Witting          | marquardt.hannah@example.com   | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | FgfXTqO20U     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 31 | Juana Robel              | sgerlach@example.org           | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | H4uzLf50K7     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 32 | Madge Reinger            | nikolaus.casey@example.org     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | EDerr7hNPd     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 33 | Emilie Okuneva           | thiel.dayne@example.org        | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 3ZS8FZ3jfW     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 34 | Lenora Wehner            | weber.justine@example.org      | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | DI7BYOGriz     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 35 | Margret Kozey            | eloy.roob@example.org          | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | hzKRTQPC7R     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 36 | Camilla Dickinson        | pfeffer.jacklyn@example.net    | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | rttglT0VAU     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 37 | Warren Larkin            | ferry.talia@example.org        | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | nxq6Wv37gK     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 38 | Katrine Corkery          | cyrus56@example.org            | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | q4XntfSKlV     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 39 | Laurel Doyle             | smayer@example.com             | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | f0ie4xxNWk     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 40 | Mrs. Vincenza Okuneva IV | mconn@example.net              | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | xoxHBfzWLe     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 41 | Connie Dach              | sflatley@example.com           | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 9KoVUGR9dE     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 42 | Rosanna Mayert           | oschultz@example.com           | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | oNHthwCqpt     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 43 | Karolann O'Keefe         | mcglynn.freddy@example.net     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 1g4kOIUih2     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 44 | Mr. Lewis Blick          | donavon78@example.org          | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 4qVkDwkvt0     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 45 | Cayla O'Keefe            | ptoy@example.org               | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | ih7l3ZSyrN     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 46 | Mr. Garry Paucek PhD     | emetz@example.org              | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | Z9kM2njxMs     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 47 | Anabel Bradtke DDS       | berniece.wilkinson@example.net | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | xehN05weHB     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 48 | Lorenza Reichel          | novella.tremblay@example.net   | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | TqHeAryuSD     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 49 | Camron Skiles DVM        | alisa.sporer@example.com       | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | rICCfdxARR     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 50 | Myrna Hickle             | lyla.marquardt@example.org     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | LlLdlKJZo3     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 51 | Dr. Myra Zemlak II       | desiree71@example.com          | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | gkHg3Vp1kq     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 52 | Dr. Alford Sanford       | predovic.tracey@example.org    | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | DoBin54Rj0     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 53 | Miss Anabel Stehr        | yasmine.conn@example.org       | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | oEaHPASlcL     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 54 | Dustin Ondricka I        | otho.hettinger@example.net     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | 8u7MrMTdiW     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
| 55 | Burley Collier           | assunta.glover@example.com     | 2023-01-15 23:11:00 | $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi | ztoBmmCpzC     | 2023-01-15 23:11:00 | 2023-01-15 23:11:00 |
+----+--------------------------+--------------------------------+---------------------+--------------------------------------------------------------+----------------+---------------------+---------------------+
52 rows in set (0.00 sec)

mysql> 

That’s it for Faker. You can explore the documentation to generate all kinds of interesting data easily. In the next article, we’ll look at creating a factory and utilizing faker. Up until now, we’ve just looked at using the already provided UserFactory. We’ll move to seeders after that.

https://github.com/dinocajic/php-youtube-tutorials

Laravel Series

Continue your Laravel Learning.

Laravel — P30: Factories

Efficient Testing in Laravel: The Role of Factories

Laravel – P30: Factories

Master Laravel factories for rapid testing & data seeding. Learn how to create realistic data models for development & testing efficiency.

Laravel — P31- Faker

Enhancing Laravel Data with Faker: Realistic Testing Made Easy

Laravel – P31: Faker Library

Elevate your Laravel testing with Faker for realistic data generation. Streamline development with dynamic, believable test data.

Laravel — P32: Factory with Faker

The Ultimate Laravel Pair: Factories and Faker for Dynamic Data

Laravel – P32: Factory With Faker

Combine Laravel factories with Faker for unparalleled testing data. Create dynamic, realistic environments for development & testing.

Leave a Reply