n Example How to generate Fake Data In Laravel 7 Database Seeder | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

In this tutorial, i will show you how to create database seeder in laravel 7 and what is  the commands to create seeder and how to run that seeder in laravel 7.

Laravel gives command to create seeder in laravel. so you can run following command to make seeder in laravel application.

Writing Seeders

 

Create Seeder Class for generate Post Data for example

php artisan make:seeder PostSeeder

this command will create one file PostSeeder.php on database/seeds directory.

Then you can write code of create posts data using model in laravel.

 

database/seeds/PostSeeder.php

<?php

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

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        for ($i = 1 ;$i < 100; $i++){
            Post::create([
                'title' => 'Lorem ipsum seeder example '.$i,
                'content' => 'Lorem Ipsum typesetting industry. Lorem Ipsum seeder example '.$i,
            ]);
        }
    }
}

 

Now you can run this seeder manually using these two ways:

  • to run single seeder use this command:
php artisan db:seed --class=PostSeeder

 

  • to run all seeders:

In this way, you have to declare your seeder in DatabaseSeeder class file.

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(PostSeeder::class);
    }
}

 

then you have to run this command for run all listed seeders.

php artisan db:seed

 

also you can  use model factories to conveniently generate large amounts of database records. and also there’s an awesome package for this called Faker to generate fake data for you. it’s already installed in Laravel! by default.

Create Model Factory For Seeding Data

 

Let’s create an PostFactory by using the following command:

php artisan make:factory PostFactory --model=Post

Above command will create PostFactory.php file inside the database/factories/ folder. We will add some attributes information into this file:

 

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Post;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence(3, true),
        'content' => $faker->sentence(5, true),
    ];
});

 

Now we will create seeder file for post model. This file is responsible to generate the fake data inside the database. The following command will generate database/seeds/PostSeeder.php file:

php artisan make:seeder PostSeeder

Now we will add below code to generate 100 records.

 

<?php

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

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

 

Above file will create 100 fake posts in the database when you run seeder. Now we will make PostSeeder entry into DatabaseSeeder.php file which is inside the database/seeds folder.

 

<?php

use Illuminate\Database\Seeder;

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

 

Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command:

composer dump-autoload

 

Now you may use the db:seed Artisan command to seed your database. By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually

php artisan db:seed

php artisan db:seed --class=PostSeeder

 

I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook