Dummy data in Laravel
Dummy or fake data generation is an important part of testing database or visualizing data in browser. Laravel being a popular framework has a few ways to create dummy data.
Now you can manually fill the database for testing but filling up data after every fresh migration is just too slow and you can do better than that. Let’s discuss two ways you might want to create dummy data.
Factory -
This is probably the more famous and used one. Factory uses faker library to generate data. In order to create fake data we have to create a factory of the same model name. Let’s create a factory for a Post
model.
First create a post model with migration.
php artisan make:model Post -m
Now add the following in database/migrations/....create_posts_table.php
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps();});
And run the migrations
php artisan migrate
Okay. now let’s create PostFactory
php artisan make:factory PostFactory
It will create database/factories/PostFactory.php
. The definition
method inside it is where we will provide the fake values.
public function definition(){ return [ 'title' => $this->faker->sentence(), 'body' => $this->faker->paragraph(), ];}
The faker library will provide us with fake value of sentence and paragraph. In order create the fake data now we just have to open tinker with php artisan tinker
and run in tinker console -
>>> Post::factory()->create() // run this line only=> App\Models\Post {#3406
title: "Sapiente odit sequi dicta est natus quae quo.",
body: "Et facere veritatis molestias deleniti tempore magni rerum. Odit dolores sit et vel laudantium ut. Distinctio occaecati veritatis vel accusamus eligendi.",
updated_at: "2021-05-31 11:21:52",
created_at: "2021-05-31 11:21:52",
id: 1,
}
And so your fake data is created and stored in database. To create multiple fake data add count(n)
method like so -
>>> Post::factory()->count(10)->create()
Seeding -
This is something I found recently. Creating dummy data by seeding is as easy as it gets. Let’s create a seeder for Post
model.
php artisan make:seeder PostSeeder
It will create database/seeders/Postseeder.php
. In the run method add fake data as -
public function run(){ Post::create([ 'title' => 'lorem ipsum dolor imit', 'body' => 'Similique molestias exercitationem officia aut. Itaque doloribus et rerum voluptate iure. Unde veniam magni dignissimos expedita eius.', ]);}
In a seeder we provide value for all the required fields. And although it seems like just adding everything yourself, it has some nice advantages which you will see. Also if you already have a factory setup like we do, you can simply use the factory rather than adding all the field data .
public function run(){ Post::factory()->create();}
Now add the PostSeeder
to database/seeders/DatabaseSeeder.php
public function run(){ $this->call([PostSeeder::class]);}
In order to store the data in database run -
php artisan db:seed
Your seed data is thus stored in database. To view it you can run Post::get()
in php artisan tinker
tinker console. After each fresh migration you can add --seed
method to run seeder’s as well.
php artisan migrate:fresh --seed
This along with the fact that seeder can be used hand to hand with factory is what makes it so great.
Conclusion -
Both factory
and seeder
method has their own advantages. Factory method is useful when you don’t need specific data, for eg.- fake users or fake posts etc. Seeder is more useful when your data is specific like if your post or product has a image url associated with it. Then you would want to use seeder even though you have to write more code. Also factory can be used with seeders and you can have your fake data generated every time with fresh migration by adding --seed
.
Resources -