Laravel Create Dummy Data using Tinker Factory Seeder and Faker

Laravel allows tinker and seeder command to generate dummy or fake data and insert into mysql database in single command.

The factory uses the Faker class to generate fake or test data and tinker and factory will insert the dummy-generated data into the MySQL database in the Laravel application.

Laravel Create Dummy Data using Tinker Factory Seedar and Faker

Steps to generate fake or dummy data into the mysql database tables using Laravel Faker:

Step 1: Create a Model & Migration File

In this step, we need to create a model and it’s migration file for generating fake data into the database table. Use the below command, it will create a migration file and model. The magic is in -m parameter.

 php artisan make:model Notes -m

And the result is that we have two files generated:

  1. app/Note.php or app/Models/Note.php
  2. database/migrations/2023_02_19_012129_create_notes_table.php

Let’s go to the app/Note.php or app/Models/Note.php and put the below code in your files.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'description',
    ];
}

Next, go to database/migrations/2013_02_19_012129_create_notes_table.php and replace the below code into your file.

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNotesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('notes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('notes');
    }
}

Step 2: Create New Factory File

In this step, we need to create a migration factory file. Let’s go to app/database/factories and inside this folder, we need to create a new file name NoteFactory.php and put the below code into your file.

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Note;
use Illuminate\Support\Str;
use Faker\Generator as Faker;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$factory->define(Note::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence($nbWords = 6, $variableNbWords = true),  // Random task title
        'description' => $faker->text(), // Random task description
    ];
});

Step 3: Generate Fake Data Using Tinker

In this step, Let’s open the command-line tool and run below command to generate fake data into database table notes

php artisan tinker

Next, run below command to generate 100 rows of random Notes.

factory(App\Note::class,100)->create();

If you are using a version of Laravel above 7, you will have to use this command:

factory(App\Models\Note::class,100)->create();

Step 7: Generate Fake Data Using Seeder

This is next method to generate fake data in database table notes using seeder.

Let’s create seeder for Note using below artisan command.

php artisan make:seeder NoteTableSeeder

The above command will generate NoteTableSeeder.php file in the database/seeds directory. Open NoteTableSeeder.php and update code shown as below.

<?php
use Illuminate\Database\Seeder;
class NoteTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Note::class, 100)->create();
        //factory(App\Models\Note::class, 100)->create(); //laravel >7
    }
}

Next step, we will declare NoteTableSeeder inside the DatabaseSeeder. The file is DatabaseSeeder located app/database/seeds.

DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(NoteTableSeeder::class);
    }
}

Next, we need to run below the PHP artisan command to generate 100 fake rows in the Note database table.

php artisan db:seed

Conclusion

In this tutorial, you have learned two methods for generating fake data using the laravel faker tinker and seeder method.

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *