Laravel 10 - How to Generate Fake Data with Laravel's Faker Library

Laravel 10 - How to Generate Fake Data with Laravel's Faker Library

Laravel 10 - Generating Fake Data with Laravel's Faker Library

Laravel provides a built-in way to generate fake data using the Faker library. This is useful for seeding databases during development and testing.

Step 1: Create a Seeder

Run the following Artisan command to create a new seeder class:

php artisan make:seeder UserSeeder

This will create a new seeder file at:
📂 database/seeders/UserSeeder.php

Step 2: Add Faker to the Seeder

Open the generated UserSeeder.php file and modify it as follows:

namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; use Faker\Factory as Faker; class UserSeeder extends Seeder { public function run(): void { $faker = Faker::create(); foreach (range(1, 10) as $index) { DB::table('users')->insert([ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, 'password' => Hash::make('password'), // Default password 'created_at' => now(), 'updated_at' => now(), ]); } } }

📌 What This Does:
✅ Generates 10 fake users.
✅ Uses Faker::create() to generate random names, emails, and timestamps.
✅ Hashes the password before inserting it into the database.

Step 3: Run the Seeder

Run the seeder with the following command:

php artisan db:seed --class=UserSeeder

This will insert 10 fake users into your users table.

Step 4: Use Factories for More Flexibility (Recommended)

Instead of manually inserting fake data using DB::table(), Laravel provides factories to generate fake data efficiently.

Create a Model Factory

Run the following command to generate a factory for the User model:

php artisan make:factory UserFactory --model=User

This will create a factory file:
📂 database/factories/UserFactory.php

Define the Fake Data

Modify the factory file:

namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\Hash; class UserFactory extends Factory { public function definition(): array { return [ 'name' => $this->faker->name(), 'email' => $this->faker->unique()->safeEmail(), 'password' => Hash::make('password'), 'created_at' => now(), 'updated_at' => now(), ]; } }

Use the Factory in the Seeder

Modify UserSeeder.php to use the factory:

use App\Models\User; class UserSeeder extends Seeder { public function run(): void { User::factory(10)->create(); // Creates 10 users } }

Run the factory-based seeder:

php artisan db:seed --class=UserSeeder

Step 5: Seed the Entire Database

To run all seeders at once, edit DatabaseSeeder.php:

public function run(): void { $this->call(UserSeeder::class); }

Then run:

php artisan db:seed

Conclusion

🎯 Using Faker with Laravel allows you to quickly populate your database for development & testing.
Direct Seeder (DB::table()) - Simple way to insert fake records.
Factory-Based Seeding (User::factory()) - More powerful and reusable.
Run Seeders using php artisan db:seed.

Would you like to add fake data for other models like Posts, Products, etc.?

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close