Categories

Friday, December 27, 2024
#919814419350 therichposts@gmail.com
LaravelLaravel 8

Database Seeding in Laravel 8 With Faker

Database Seeding in Laravel 8 With Faker

Hello to all, welcome to therichpost.com. In this post, I will do, Database Seeding in Laravel 8 With Faker.

In this post, I will tell you, How to work with Database Seeding in Laravel 8. 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 users before done seeder:

Database seeder
Laravel Database seeder

Here is the complete and easy process for Database Seeding in Laravel 8 With Faker:

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\seeders\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:

Database Seeding in Laravel 8 With Faker
Database Seeding in Laravel 8 With Faker Package

Now we are done friends and please run your Laravel 8 project and see the Fake Data.  Also and If you have any kind of query or suggestion or any requirement then feel free to comment below.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. I will come with more Laravel 8 demos in my coming posts.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

Jassa

Thanks

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.