Using Fake Helper in Laravel

Laravel version 9.18 has introduced a fake helper, which can be used to generate fake data in your application. This can be used while UI prototyping or generating test data for testing and generating factory and seed data.

Using fake helper in Blade file


<table>
            <thead>
                <tr>
               <th>Name</th>
               <th>Address</th>
               <th>Phone Number</th>
               </tr>
            </thead>
            <tbody>
             @for($i=0; $i<=10; $i++)
                <tr>
                    <td>{{fake()->name}}</td>
                    <td>{{fake()->address}}</td>
                    <td>{{fake()->phoneNumber}}</td>
                </tr>
                @endfor
            </tbody>
        <table>

This should generate following output in your blade file

Using fake helper in Factory

fake helper can also be used in Factory classes to generate fake data.


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