Categories

Thursday, March 28, 2024
#919814419350 therichposts@gmail.com
Laravl 5.7Php

Database Seeding in Laravel 5.7

Laravel 7.2 routing with route group auth guard check with prefix

Database Seeding in Laravel 5.7 - The Rich Post

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:

before laravel 5.7 seeding

 

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:

laravel seeder file

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:

after laravel 5.7 seeding

 

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 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

3 Comments

Leave a Reply

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