Laravel 8 Factory Tinker Example Tutorial

In this tutorial you will learn about the Laravel 8 Factory Tinker Example Tutorial and its application with practical example.

In this Laravel 8 Factory Tinker Example Tutorial I will show you how to generate and insert dummy or fake records in your database table using laravel factory tinker in laravel 8 application.In this tutorial you will learn how to generate fake records in laravel using laravel factory tinker. You will also learn how to use laravel factory tinker. With Laravel factory tinker, you can generate bulk amount of records and insert into the database. In this tutorial you will also learn to use PHP Faker library in laravel to generate fake data. PHP Faker is a powerful PHP library that helps you to generate fake data for while developing new application.

In Laravel 8 we will be using “laravel/tinker” composer package to generate dummy records. Laravel 8 comes with “laravel/tinker” package. In database/factories folder you can add your different factory for different model.

Laravel 8 Factory Tinker Example Tutorial

In this step by step tutorial I will demonstrate you how to generate and insert dummy or fake records in your database table using laravel factory tinker. Please follow the instruction given below:

Install Laravel 8

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

Make sure you have composer installed.

Setup Database Credentials

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.

Generate Model & Run Migration

Now, in this step we will create model and migration file. Please run the following command:

Once above command is executed there will be a migration file created inside database/migrations/ directory, just open migration file and update the function up() method as following:

Now, go to app/Product.php and open Product model file and put the below code.

app/Models/Product.php

Create Custom Factory:

In this step you need to create a custom factory class using following artisan command:

Now open database\factories\ProductFactory.php file and put the following code in it:

database\factories\ProductFactory.php

Now, run following command in termina:

Generate Dummy Records with Factory Tinker

FInally we will be creating test data and seed them into the Product table. Use the following command:

In this tutorial we have learn about the Laravel 8 Factory Tinker Example Tutorial and its application with practical example. I hope you will like this tutorial.