Laravel 8 Factory Tinker Example

Jun 30, 2021 . Admin

Hi Artisan

Today, In this article, I will explain you how to create dummy records in your database table using tinker factory of Laravel 8.

Sometime we need to integrate millions of records in your employee table, OR maybe thousands or millions of records. Also think about if you need to use pagination. then you have to add some records for testing. So what you will do it at that that moment, You will add manually thousands or millions of records ? What you do ?. If you add manually thousands of records then it can be take more time.

Here,Laravel 8 have composer package "laravel/tinker" that we will provide you to generate dummy records by using their command. So Laravel 8 by default take "laravel/tinker" package for project. Also they provide by default one factory for user table. You can check path here : database/factories/. In this folder you can add your different factory for different model.

Generate Dummy Employee:
php artisan tinker
Employee::factory()->count(6)->create()

Here, I will give you full example for simply create dummy data using tinker factory using laravel 8 as bellow.

Step 1 : Install Laravel 8 Application

First of all we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Model and Migration

Here this step, we will create one model and migration name Employee. Use the below following command and create it.

php artisan make:model Employee -m

next,open employees migration file and put the below code.

Path: /database/migrations/2021_06_30_041820_create_employees_table.php
<?php

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

class CreateEmployeesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('employees');
    }
}

Next, go to app/Models/Employee.php and open Employee model file and put the below code. here following path of model fille

Path:/app/Models/Employee.php
<?php

namespace App\Models;

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

class Employee extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'email',
    ];
}

Step 3: Create Factory

In this setp,I will create EmployeeFactory in following command As you see above command for user, But you can not do same for other model. If you want to do this then you have to add factory for that model. So i am going to give you full example of factory from scratch.

php artisan make:factory EmployeeFactory --model=Employee

I have Employee model with employees table. Now we want to add dummy records for Employee model. So we have to add factory like as bellow :

Path : database/factories/EmployeeFactory.php
<?php

namespace Database\Factories;

use App\Models\Employee;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class EmployeeFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Employee::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'email' => $this->faker->email,
        ];
    }
}

after run command in terminal

composer dump-autoload
and
php artisan tinker
Employee::factory()->count(6)->create()

So i hope you created your 6 dummy records on employees table.

Output

It Will help you...

#Laravel 8 #Laravel