Laravel 5 database seeder example with insert sample user data

Laravel 5 database seeder example with insert sample user data

In this post we will give you information about Laravel 5 database seeder example with insert sample user data. Hear we will give you detail about Laravel 5 database seeder example with insert sample user dataAnd how to use it also give you demo for it if it is necessary.

In this tutorial, I will let you know what is database seeder, what is the use of database seeder and how to generate seeder file with migrations in Laravel 5.

In this example, I will seed some test/dummy data into users table that will not created by any users.

For testing any applications where you will need different user’s credentials to test all the scenarios, then you can populate the data by using seed functionality provided by Laravel.

You can generate the seeder class by running following command :

See also  How to Make a Discord Bot in Node.js

php artisan make:seeder UsersTableSeeder

Once above command will be executed successfully then you will get “UsersTableSeeder” class in database/seeds directory, All the seeders class generated with artisan command will be place in the database/seeds directory.

There will be only one method in seeder class by default :run.

Within the run method, I will write the insert query to load some user data into users table.

Ok, let’s open the “UsersTableSeeder.php” file and add the following line of code to insert user data :


database/seeds/UsersTableSeeder.php

  1. <?php
  2. use IlluminateDatabaseSeeder;
  3. class UsersTableSeeder extends Seeder
  4. {
  5. /**
  6. * Run the database seeds.
  7. *
  8. * @return void
  9. */
  10. public functionrun()
  11. {
  12.     DB::table('users')->insert([
  13. 'name'=>str_random(6),
  14. 'email'=>strtolower(str_random(6)).'@gmail.com',
  15. 'password'=>bcrypt('test@123')
  16. ]);
  17. }
  18. }
<?php

use IlluminateDatabaseSeeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => str_random(6),
            'email' => strtolower(str_random(6)).'@gmail.com',
            'password' => bcrypt('test@123')
        ]);
    }
}

The above code will insert one record at a time, If you need to insert in bulk then put the code into a loop.

Now run the following command to seed the data :

php artisan db:seed --class=UsersTableSeeder

When you run the artisan command with db:seed option without specifying any class, then this will run the DatabaseSeeder class.

You can break seeders into multiple files and call them in default “DatabaseSeeder” class by using call method. call method is used to call additional seed class.

  1. <?php
  2. use IlluminateDatabaseSeeder;
  3. class DatabaseSeeder extends Seeder
  4. {
  5. /**
  6. * Run the database seeds.
  7. *
  8. * @return void
  9. */
  10. public functionrun()
  11. {
  12. $this->call(UsersTableSeeder::class);
  13. }
  14. }
<?php

use IlluminateDatabaseSeeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
    }
}

Now you can run following command:

php artisan db:seed

Try this.. 

Hope this code and post will helped you for implement Laravel 5 database seeder example with insert sample user data. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  59  =  63

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US