Laravel 8 Factory Create Dummy Data Example

Laravel 8 Factory seeders example. In this post, I would like to show you how create/generate dummy data into database table using the factory and seeders in laravel 8 app.

If you want to add thousand of dummy or fake records into database, so you can easily geneate or create dummy records into database table using factory seeders and faker in laravel 8. So, you will not add manually thousands of records then it can be take more time.

And you will learn step by step how to generate or create fake or dummy records into database using factory, faker and seeders in laravel 8 with terminal/command prompt.

Generate dummy data in Laravel 8 using Model Factory

  • Step 1 – Install Laravel 8 App
  • Step 2 – Database Configuration
  • Step 3 – Create Model and Migration
  • Step 4 – Create Factory
  • Step 5 – Create Dummy Data By Factory Command

Step 1 – Install Laravel 8 App

In step 1, install or download laravel 8 application, so open terminal or command prompt and run the following command to install fresh laravel 8 app:

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

Step 2 – Database Configuration

In step 2, configure database details in .env file:

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

Step 3 – Create Model and Migration

In step 3, 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 located inside /database/migrations/ directory. And add the following code in it:

<?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->text('description');
            $table->timestamps();
        });
    }

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

Then, open Post.php file which is located inside app/Models directory. And add 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'
    ];
}

Step 4 – Create Factory

In setp 4, create factory class named PostFactory by using the following command:

php artisan make:factory PostFactory --model=Post

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

<?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

Step 5 – Create Dummy Data By Factory Command

In step 5, open command prompt and run artisan tinker command and then run create dummy data factory command to generate dummy data into database table:

php artisan tinker

Post::factory()->count(20)->create()

Be First to Comment

Leave a Reply

Your email address will not be published.