Laravel 9 Generate Dummy Data Using Factory Tutorial

In this tutorial you will learn about the Laravel 9 Generate Dummy Data Using Factory Tutorial and its application with practical example.

In this Laravel 9 Generate Dummy Data Using Factory Tutorial, I will show you how to generate dummy data using factory, faker, tinker and seeder in laravel 9 application. In this tutorial you will learn to generate fake or dummy data in a database table using factory in laravel 9. While working with any web application using laravel we come situations where we need some dummy or test data to available in database. Laravel factory makes this job much easier using laravel factory we can generate dummy data into the database table.

Laravel 9 Generate Dummy Data Using Factory Tutorial

In this tutorial, you will learn how to generate dummy data into the database tables using factory in laravel 9 application. Please follow the instruction give below:

  • Install Laravel 9
  • Setup Database Credential
  • Create a Model & Migration File
  • Create New Factory File
  • Generate Fake Data Using Tinker

Install Laravel 9

First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command

Configure Database

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

.env

Create Model & Migration

Now, we have to define table schema for posts table. Open terminal and let’s run the following command to generate a migration along with model file to create posts table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –

Next, go to app/Models/Post.php file and add the $fillable property in the Post model as following.

Now, run following command to migrate database schema.

After, the migration executed successfully the posts table will be created in database.

Create New Factory File

In this step, we will create a factory file. Run the following command to create factory class named PostFactory by using the following command:

Go to app/database/factories and open PostFactory.php and put the below code into your file.

Run the following command to auto load dependencies.

Generate Fake Data Using Tinker

Please use the following command to generate fake data using tinker:

Now, run following command to generate 100 rows of random Notes.

In this tutorial we have learn about the Laravel 9 Generate Dummy Data Using Factory Tutorial and its application with practical example. I hope you will like this tutorial.