Hello to all, welcome to therichpost.com. In this post, I will do, Database Seeding in Laravel 5.7 With Faker Package.
In this post, I will tell you, How to work with Database Seeding in Laravel 5.7 With Faker Package, 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 to create fake data. I personally like this Laravel feature very much.
Here is database table image before done seeder:
Here is the complete and easy process for Database Seeding in Laravel 5.7 With Faker Package:
1. Like others, we don’t need to install Laravel Faker Package because it is already installed, here you can see in composer.json file:
Laravel gives us default:
2. Second, we just need to run below code into database\seeds\DatabaseSeeder.php file:
In this file, we call laravel insert query to create fake data:
<?php use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Faker\Factory as Faker; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { $faker = Faker::create(); foreach (range(1,10) as $index) { DB::table('users')->insert([ 'name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt('123456'), ]); } } }
3. In the end, we need to run below command in terminal:
This command will seed database with fake data:
$ 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.
Recent Comments