Hello to all, welcome to therichpost.com. In this post, I will do, Database Seeding in Laravel 5.7.
In this post, I will tell you, How to work with Database Seeding in Laravel 5.7 which creates fake data for us. Like I always tell, Laravel has many unique features, which makes our coding easy and faster but for this we have to learn laravel well. Mostly we use laravel Model Factories and Faker Package to create fake data but I used to feed laravel database in simple way.
Here is database table image before done seeder:
Here is the complete and easy process for Database Seeding in Laravel 5.7:
1. First, you need to run below command to create seeder in laravel 5.7:
$ php artisan make:seeder UserTableSeeder
2. After run seeder command into your terminal, you will find file in database/seeds/UserTableDataSeeder.php:
In this, you need to add following code:
<?php use Illuminate\Database\Seeder; class UserTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { DB::table('users')->insert([ 'name' => str_random(10), 'email' => str_random(10).'@gmail.com', 'password' => bcrypt('123456'), ]); } }
3. Then after, you need to add following code into database\seeds\DatabaseSeeder.php file:
In this file, we will call the UserTableSeeder Class.
<?php use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { $this->call(UserTableSeeder::class); } }
4. In the end, we need to run below command in terminal:
$ php artisan db:seed
After run above command, you can your database table Users with new data:
If you have any query related to this post, then do comment or ask questions.
You missed Faker package without which this article is incomplete
Please fix class name typos in seede names
Otherwise nice start
Thank you for you comment and I will also add faker Package later. 🙂
great also try this instead of random:
DB::table(‘users’)->insert([
‘name’ => ‘ajay’,
’email’ => ‘ajay@gmail.com’,
‘password’ => bcrypt(‘123456’),
]);