Laravel – Example of Database Seeder with insert sample data

Laravel – Example of Database Seeder with insert sample data

In this post we will give you information about Laravel – Example of Database Seeder with insert sample data. Hear we will give you detail about Laravel – Example of Database Seeder with insert sample dataAnd how to use it also give you demo for it if it is necessary.

We should know what is database seeder in laravel and why we should use before start example of database seeding. Laravel provides a tool to add sample or dummy data to our databases automatically. that is call it database seeding.

Laravel database seeder through we can add simply testing data on our database table. Database seed is extremely useful because testing with various data allows you to likely detect bugs you otherwise would have overlooked. We have to simple make one time seeder with some dummy data, that way we can simply reuse when you deploy project first time. We can make seeder after migration of table.

So today in this example we will add some sample data of users table from scratch. So laravel provide us command for creating seed and run that. so first run bellow command for create “UsersTableDataSeeder” seeder.

Make Seeder

php artisan make:seeder UsersTableDataSeeder

After run above command successfully, you will be found new created file “database/seeds/UsersTableDataSeeder.php”. In this file i make three sample user for users table using insert query. So open UsersTableDataSeeder.php file and some sample data like as bellow:

database/seeds/UsersTableDataSeeder.php

<?php


use IlluminateDatabaseSeeder;


class UsersTableDataSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

for ($i=0; $i

DB::table('users')->insert([

'name' => str_random(8),

'email' => str_random(12).'@mail.com',

'password' => bcrypt('123456')

]);

}

}

}

Now we are ready to run above seeder using bellow command:

Run Seeder:

Also see:Laravel – Multiple Files Download with Response Example

php artisan db:seed --class=UsersTableDataSeeder

I hope it can help you…

Hope this code and post will helped you for implement Laravel – Example of Database Seeder with insert sample 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 *

  +  37  =  45

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