We need the test data to test our program. We need to test every test case with the required test data. Laravel provides a feature called Laravel Factory and Seeder to create your test data quickly. So, you do not have to spend hours creating test data manually to satisfy the program logic.
In this topic, we will show how to use Laravel Factory and Seeder to create fake or dummy data in the MySQL database. When you develop an application, you need proper test data to test it. Factory and Seeder are Laravel Libraries using which you can create test data easily.
We will use a custom table and create test data using Laravel Factory. Create a factory on that table and update DatabaseSeeder.php
to call the factory. Then run the database seeder. Let us use the below steps to complete creating test data.
- Create a Laravel 10 project
- Create a MySQL database migration
- Create a Laravel factory file for the newly created table
- Update
DatabaseSeeder.php
- Run
db:seed
- Verify data in the table
1. Create a Laravel 10 project
We will create a project in Laravel 10. Make sure you have Composer and PHP installed in your system. We will name the project as lara_seeder. Run the below command from the terminal.
composer create-project --prefer-dist laravel/laravel lara_seeder
This will create a folder named "lara_seeder" under the folder where you run the above command.
2. Create a MySQL database migration
Update .env
file for database details. We are using a MySQL database named 'lara_demo', so we updated the file as below:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lara_demo
DB_USERNAME=root
DB_PASSWORD=
We will create a new table called "applications" for all the registered applicants. You can use an existing table like "users" to populate test data into it. Then you do not need to create a new table, you can directly create the factory file.
Let us create a Laravel migration file for this table. Go to your project folder and run the below command from the VS code terminal.
php artisan make:model Application -m
Update migration file
Let us update the above migration file as below:
public function up(): void
{
Schema::create('applications', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('address')->nullable();
$table->timestamps();
});
}
Now run Laravel migration to create the default Laravel tables and our custom "applications" table. Run the below command from the terminal to create the tables.
php artisan migrate
After running the migration see the structure of 'applications' table.
3. Create a Laravel factory file for the newly created table
Create a factory for the Application model as below:
php artisan make:factory ApplicationFactory --model=Application
Update ApplicationFactory as below:
public function definition(): array
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'address' => $this->faker->address,
];
}
4. Update DatabaseSeeder.php
Now, update the run() method of DatabaseSeeder.php
under the database/seeders folder to call the factory we created. We want to create 100 rows in the "applications" table.
public function run(): void
{
\App\Models\Application::factory(100)->create();
}
5. Run db:seed
Now, run Laravel database seeder to create the rows:
php artisan db:seed
6. Verify data in the table
You can see that 100 rows are created in the "applications" table.
Post a Comment