Laravel 8 Generate Dummy Data Using Factory Tutorial

  2618 views   1 year ago Laravel

It is important to test application with adequate database. When we first create application, we don't have data. We have to create lots of dummy data with automation. Laravel 8 comes a composer package laravel/tinker that we will help you to generate dummy records by using their command.

In this tutorial, I will show you how to generate dummy records in your database table using tinker factory of Laravel 8. We will create application step by step from the scratch.

  • Step: 1 Create Laravel 8 application
  • Step: 2 Database configuration in .env file
  • Step: 3 Create model and migration file
  • Step: 4 Create factory

Follow the below steps to create dummy data using tinker factory in laravel 8.

Step 1 : Create Laravel 8 application

We need to get fresh Laravel application using below command, So open your Terminal or Command Prompt and run below command:

composer create-project --prefer-dist laravel/laravel products

Step 2: Database configuration in .env file

In the second step, we need to configure database in .env file at the root of application. Change it according to your mysql credentials.


Step 3: Create model and migration file

In the third step, we will create model and migration file. For that, use the below command and create it.

php artisan make:model Product -m

This will create migration file at database/migrations folder. Open the migration file and add the below lines in it.


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('products', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

We also need to add $fillable property into Product model. So open Product model at app/Models folder and add $fillable property with table fields.


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
    use HasFactory;

     * The attributes that are mass assignable.
     * @var array
    protected $fillable = [

Now run the following migrate command to create products table into database.

php artisan migrate

Step 4: Create factory

In this step, we will create factory for Product model. To create factory, run the below command into Terminal:

php artisan make:factory ProductFactory --model=Product

This will create factory class at database/factories folder. Now we need to add products tatble fields with datatype we want to add into table. So open factory file and change it as below.


namespace Database\Factories;

use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;

class ProductFactory extends Factory
     * The name of the factory's corresponding model.
     * @var string
    protected $model = Product::class;

     * Define the model's default state.
     * @return array
    public function definition()
        return [
            'name' => $this->faker->name,
            'description' => $this->faker->text,

You might need to dump-autoload composer using below composer command into Terminal.

composer dump-autoload

Now, we need to run the factory using Laravel tinker. So from Terminal, first start tinker using command:

php artisan tinker

And create as much dummy data as you want. Just pass number into count() method.


This will create 100 products dummy records.

This way, you can create dummy data using tinker and factory into Laravel 8. Thanks for giving time to reading the article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]