How To Use Factory in Seeder Laravel 8 Tutorial

Share this Article
Reading Time: 5 minutes
11,601 Views

Inside this article we will see the concept of Factory in seeder of laravel 8 tutorial. Using factory with faker library to seed fake data into database table.

This tutorial will be super easy to understand and easy to implement in your code. This tutorial will guide you to understand How to use factory in seeder laravel 8 tutorial. Factory is the feature of laravel artisan.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

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

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Create Model & Migration

Open project into terminal and run this command to create model & migration file.

$ php artisan make:model Post -m

It will create two files –

  • Model – Post.php at /app/Models folder
  • Migration file – 2021_07_25_123805_2021_08_10_120110_create_posts_table at /database/migrations folder.

Open Migration file and write this complete code into 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", 50);
            $table->text("description");
        });
    }

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

Open model file Post.php and write this complete code into it.

<?php

namespace App\Models;

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

class Post extends Model
{
    use HasFactory;

    public $timestamps = false;
}

Run Migration

Next, we need to create table inside database.

$ php artisan migrate

Create Factory

Next, we need to create a factory file in which we will use faker library which generates fake data.

$ php artisan make:factory PostFactory --model=Post

It will create a file PostFactory.php at /database/factories folder.

Open PostFactory.php and write this complete code into it.

<?php

namespace Database\Factories;

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

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,
        ];
    }
}

Create Seeder

Next, we need to create a seeder file.

$ php artisan make:seeder PostSeeder

It will create a file PostSeeder.php at /database/seeders folder.

Open PostSeeder.php and write this complete code into it.

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Post;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Post::factory()->count(50)->create();
    }
}

Data Seeding

Open project into terminal and run this artisan command

$ php artisan db:seed --class=PostSeeder

It will seed countries data into table.

We hope this article helped you to learn How To Use Factory in Seeder Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5