Generating fake data manually for each model seed is cumbersome. Instead, you can use model factories to generate large amounts of database records conveniently. Laravel has a feature called model factories that allow us to generate fake data. It is beneficial for testing and seeding fake data into the database to see the code in action before any real-time user data comes into your application. Let’s deep dive into the Model Factories in Laravel.

Creating Migration

Faker is the PHP library that generates fake data for you. Whether you need to bootstrap your database, fill-in your persistence to stress test it or anonymize data taken from the production service, Faker is for you. By default, Laravel installs the faker library as a dependency.

Assume that you are building an Blog application in Laravel which have Model Post.

We have generated a model file with the migration looks like below.

Post

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->text('body');
    $table->timestamps(); });

Now you can create your database by running the following artisan command.

php artisan migrate

Database Seeding with Model Factories

Now let’s get started with database seeding. We will populate the database tables with Model Factories, Model Factories are located at folder database/factories.

If you have generated the Auth Scaffolding you should already have a file named UserFactory.php in your factories folder with content

UserFactory.php

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;


$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});

Factories provide a convenient way to generate new model instances for testing / seeding your application’s database. It makes use of Faker Library to generate arbitrary data for you. For more details on Faker library visit this link.

Let’s now go ahead and create a new Model Factory for our Post Model. Navigate to your project root in the terminal / command prompt and run the following command

php artisan make:factory PostFactory --model=Post

This artisan command generates a new Factory file named PostFactory in the factories directory.

Let’s now modify this file, to generate dummy data for columns in Posts table.

$factory->define(App\Post::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence,
        'body'  => $faker->paragraph
    ];
});

We are now ready with our Model factories, To Seed the database open tinker. Go to your terminal and navigate to project root folder and run following command

php artisan tinker

To generate Posts execute following command

$posts = factory('App\Post', 50)->create();

This will create 50 Posts in your database posts table.

Now I think you will understand how the model factories work.

For more details see the Laravel documentation.