Categories

Friday, December 20, 2024
#919814419350 therichposts@gmail.com
Laravl 5.7Php

Database Seeding in Laravel 5.7 With Faker Package

Laravel 7.2 routing with route group auth guard check with prefix

Database Seeding in Laravel 5.7 With Faker Package - The Rich Post

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:

laravel Faker

 

 

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:

laravel faker data

 

If you have any query related to this post, then do comment or ask questions.

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.