This is another laravel tutorial for seeding fake data using the factory. We will generate dummy data using a model and seed them into db.
There is some fake data is needed for testing in the prod environment. You can enter data manually or generate using script into db.
The laravel provides an option to generate sample data in a gentle way. Laravel has a feature called model factories that allows us to generate fake data.
The libraries fzaninotto/faker
help to generate fake data, we can create a lot of fake data using this libs. You do not need to install this package if you are using laravel 9 or higher. You can install faker libs using the composer command –
composer require fzaninotto/faker --dev
After successfully installing faker, we will create our user seeder class to generate user fake data.
We will create a persons models factory using the following command –
php artisan make:model Person -m
The above command will create persons_table.php
file into the migration table. The following code will add into up()
method.
public function up() { Schema::create('persons', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('firstName'); $table->string('lastName'); $table->timestamps(); }); }
Now migrate using the below command –
php artisan migrate
Let’s create an PersonFactory by using the following command.
php artisan make:factory PersonFactory
The above command will create PersonFactory.php file inside the database/factories/ folder. We will add some attributes information into this file –
// PersonFactory.php use Faker\Generator as Faker; use App\Person; $factory->define(person::class, function (Faker $faker) { return [ 'name' => $faker->name, 'firstName' => $faker->firstName, 'lastName' => $faker->lastName ]; });
Whereas –
We have defined a factory and passed Person.php
model as a first parameter. Then we have the callback function that defines the data and returns it in an array.
We will create a seeder file for the person model. This file is responsible to generate the fake records inside the database. The following command will generate PersonsTableSeeder.php
file –
php artisan make:seeder PersonsTableSeeder
The file will generate into database/seeds
folder. We will add the below code to create dummy data –
<?php use Illuminate\Database\Seeder; use App\Person; class PersonsTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { $count = 100; factory(Person::class, $count)--->create(); } }
The above file will create 100 fake records in the database when you run the seeder. Now we will make PersonsTableSeeder
entry into DatabaseSeeder.php
file which is inside the database/ seeds folder.
use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { $this->call(PersonsTableSeeder::class); } }
Now, we will regenerate the Composer’s autoloader using the following dump-autoload
command –composer dump-autoload
We will seed data into a person table using the below command.
php artisan db:seed
Above command will generate 100 fake records inside the persons table.
The unit testing will cover in the next laravel tutorial.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More