Skip to main content

Create a Seeder: Generate a seeder class using Artisan that will insert the dummy user

To insert a dummy user for testing purposes so you don't need to insert it manually each time you run `php artisan migrate:fresh`, you can use Laravel's seeding feature. 


Here's a step-by-step guide to achieve this:


1. **Create a Seeder**: Generate a seeder class using Artisan that will insert the dummy user.


   ```bash

   php artisan make:seeder UserSeeder

   ```


2. **Define Seeder Logic**: Update the `UserSeeder` class to insert the dummy user. Open `database/seeders/UserSeeder.php` and add the following code:


   ```php

   <?php


   namespace Database\Seeders;


   use Illuminate\Database\Seeder;

   use Illuminate\Support\Facades\DB;

   use Illuminate\Support\Str;

   use Illuminate\Support\Facades\Hash;


   class UserSeeder extends Seeder

   {

       /**

        * Seed the application's database.

        *

        * @return void

        */

       public function run()

       {

           DB::table('users')->insert([

               'user_id' => Str::uuid(),

               'first_name' => 'John',

               'middle_name' => 'D',

               'last_name' => 'Doe',

               'email' => 'john.doe@example.com',

               'password' => Hash::make('password'), // Use a default password or any secure value

               'role_id' => 1,

               'created_at' => now(),

               'updated_at' => now(),

           ]);

       }

   }

   ```


   In this example:

   - `Str::uuid()` generates a unique UUID for `user_id`.

   - `Hash::make('password')` hashes the password for security.

   - `now()` sets the current timestamp for `created_at` and `updated_at`.


3. **Call Seeder from DatabaseSeeder**: Open `database/seeders/DatabaseSeeder.php` and call the `UserSeeder` within the `run` method:


   ```php

   <?php


   namespace Database\Seeders;


   use Illuminate\Database\Seeder;


   class DatabaseSeeder extends Seeder

   {

       /**

        * Seed the application's database.

        *

        * @return void

        */

       public function run()

       {

           // Call the UserSeeder

           $this->call(UserSeeder::class);

       }

   }

   ```


4. **Run the Migrations and Seeders**: To ensure that your migrations and seeders run properly, you should use the following command:


   ```bash

   php artisan migrate:fresh --seed

   ```


   This command will drop all tables, re-run the migrations, and then seed the database with the dummy user.


### Summary

With this setup, you won't need to manually insert the dummy user each time you run `php artisan migrate:fresh`. Instead, the dummy user will be automatically inserted by the seeder. This approach is more efficient and keeps your testing process streamlined.


In Laravel, if you want to run a specific seeder class instead of seeding all files with php artisan migrate:fresh --seed, you can use the --seeder option.


php artisan db:seed --class=YourSeederClassName

For example, if your seeder file is named UsersTableSeeder.php (located in database/seeders), you would run:

php artisan db:seed --class=UsersTableSeeder

If you want to drop all tables, migrate, and then run only a specific seeder:

php artisan migrate:fresh --seeder=UsersTableSeeder

This avoids running DatabaseSeeder.php and lets you control exactly which seeder runs.

Comments

Popular posts from this blog

Business hours calculation using PHP

Most of ticketing alert projects needed SLA calculation to exclude holidays and weekends or Off business hours. First of all, let us take a look at types of SLA we need to calculate. For instance, few companies will work 24/7 shifts and 8x5, 8x7 etc. So we need to consider business hours. In this example, I'm considering business hours which starts at 09:00 AM and ends at 06:00 PM. Also, using this code we can calculate 24/7 too. Let's consider business hours started at 9:00 AM to 18:00 PM without weekends. Now, take the actual work start period and end period.                                                       Demo <?php $incident_work_started = '2018-09-12 09:28:07'; $incident_work_ended = '2018-09-19 17:45:00'; $totalHours = round(get_working_hours($incident_work_started,$incident_work_ended),2)."\n"; e...

Laravel all artisan commands and uses

Laravel all artisan commands and uses  Laravel commands Have you all know, Laravel run on mostly PHP artisan commands. here I'm going to provide a quick image that show all artisan commands on Laravel.  If do you want to check all Laravel commands, Just run the command on your CMD php artisan You will get all the commands 

Select 2 options with live examples

Select2 options Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options. Here I prepared for all set of examples with sample codes, You don't need to read entire document to do this See the Pen Select2 examples from Codesponsors.com by Codesponsors ( @Codesponsors ) on CodePen .