Laravel 9 Factory – Generate Dummy Data using Faker Tutorial

Laravel 9 Factory, In this tutorial you will learn how to generate dummy or fake data into database table using factory, faker, tinker or seeder in laravel 9 app.

For the testing the application we must need fake records in the database. So, we need to add manually entering fake records in the database.

In Laravel factory, faker, seeders, and tinker command has been given to generate and create the Laravel fake and dummy data in the database.

Laravel Generate Fake Data using Faker and Factory

Follow the following steps to create or generate fake data for inserting database table using faker and factory command in laravel:

  • Step 1: Install Laravel App
  • Step 2: Connecting Database to App
  • Step 3: Create Model & Migration
  • Step 4: Create Factory Class
  • Step 5: Run Factory, Tinker Command

Install Laravel App

In this step, we need to run the below command to download or install a fresh laravel setup. So open terminal and run the following command:

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

Connecting App to Database

In this step, we need to navigate laravel dynamic google pie chart app project root directory. And open .env file. Then add database detail like below:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Create Model and Migration

Create one model and migration name Post using the following command:

php artisan make:model Post -m

After that, open create_posts_table.php file, which is found inside /database/migrations/ directory and update the following code in it:

database\migrations\2021_09_26_072241_create_posts_table.php

<?php

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

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

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

app\Models\Post.php

Next, go Post.php modal inside app/Models directory and update the following code into it:

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasFactory;
    protected $fillable = [
        'title', 'description'
    ];
}

After that run the migrate command for generating database table.

php artisan migrate

Create Factory Class

In this step, open terminal again and run the following command to create a controller named PostFactory.php:

php artisan make:factory PostFactory --model=Post

After that, open PostFactory.php file, which is found inside database/factories/ directory. And update the following code in it:

database\factories\PostFactory.php

<?php
 
namespace Database\Factories;
 
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
 
class PostFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Post::class;
 
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => $this->faker->title,
            'description' => $this->faker->text,
        ];
    }
}

The run the following command on command prompt to auto load dependencies:

composer dump-autoload

Run tinker, Factory Command

Execute the following command on command prompt to generate or create dummy data using tinker and factory command:

php artisan tinker
 
Post::factory()->count(20)->create()

Now, visit localhost/phpmyadmin and check database posts table where you can see the test data is reflacted.

2 thoughts on “Laravel 9 Factory – Generate Dummy Data using Faker Tutorial”

Leave a Comment