Database Seeding or upload fake data using faker library in laravel

Database Seeding or upload fake data using faker library in laravel

Database Seeding or upload fake data using faker library in laravel

Database seeding is a process for filling up database with dummy text. Here we use faker for dummy text. Faker is a PHP library that generates fake data 

for your database table. So lets start and create dummy data for our "posts" table. For example in our "posts" table have to column

title and body.

So lets's create our first seeder:

$ php artisan make:seeder PostsTableSeeder

Seeders file location your project/database/seeds directory. Here’s how it looks like after we set it up to create a few data


class PostsTableSeeder extends Seeder
{
    public function run()
    {
        // Let's truncate our existing records to start from scratch.
        Post::truncate();
        $faker = \Faker\Factory::create();
        // And now, let's create a few articles in our database:
        for ($i = 0; $i < 50; $i++) {
            Post::create([
                'title' => $faker->sentence,
                'body' => $faker->paragraph,
            ]);
        }
    }
}


So let’s run the seed command as blew and it will insert some dummy data as we say in our code above:

$ php artisan db:seed --class=ArticlesTableSeeder


Done Now Open http//:localhost/phpmyadmin and click on your posts table see your dummy data.


In that way we can create some dummy user in our User table

Let’s repeat the process to create a Users seeder:


class UsersTableSeeder extends Seeder
{
    public function run()
    {
        // Let's clear the users table first
        User::truncate();
        $faker = \Faker\Factory::create();
        // Let's make sure everyone has the same password and 
        // let's hash it before the loop, or else our seeder 
        // will be too slow.
        $password = Hash::make('123456');
        User::create([
            'name' => 'Administrator',
            'email' => '[email protected]',
            'password' => $password,
        ]);
        // And now let's generate a few dozen users for our app:
        for ($i = 0; $i < 10; $i++) {
            User::create([
                'name' => $faker->name,
                'email' => $faker->email,
                'password' => $password,
            ]);
        }
    }
}


We can make it easier by adding our seeders to the main DatabaseSeeder class inside the database/seeds folder:

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call(ArticlesTableSeeder::class);
        $this->call(UsersTableSeeder::class);
    }
}

This way, we can simply run

$ php artisan db:seed

and it will run all the called classes in the run() method.

Now check again your database table we have successfully upload dummy users and contents in posts table.


Thanks for reading please share with your friends & family who love coding in laravel



Comments


  • database seeding
  • seeder
  • laravel database seeder
  • dummy data
  • using faker
  • laravel faker