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

Laravel form validations

 Laravel Validations: List of types "first_name" => 'required|alpha:ascii|min:3|max:100',// alpha:ascii (only accepts a-z) "middle_name" => 'string', "last_name" => 'required|string', "email" => 'required|email|unique:users,email', "password" => 'required|string|confirmed', "sex" => 'required|string', "phone_no" => 'required|string', "account_type" => 'required|string', "dob" => 'required|date_format:d-m-Y', // date with format "nationality" => 'required|string', "company" => 'required|string', "company_sector" => 'required|string', "company_address" => 'required|string' "bank_account_no" => 'required|min_digits:3|max_digits:5', "role" => 'required|in:admin,editor,viewer', ...

Data Types in Python

Data Types  In C# or Java, You need to declare a variable specify them integer, string, and decimal. But in Python no need to specify. We can declare variables like Example: C# or Java int age = 28; string Name = "Siddhu"; Example: Python age = 28 Name = "Siddhu" So, you don't need to declare variable types in python. This is an advantage in Python, But still have few Disadvantages too. Example: In my Python function def add_numbers(x,y): print(x+y) add_numbers(20,50) //Output: 70 add_numbers(20,"Something") //Error:"Traceback (most recent call last): File "C:/Users/siddhartha.e/PycharmProjects/siddhu-py/my1stpycode.py", line 8, in add_numbers(50,"Something") File "C:/Users/siddhartha.e/PycharmProjects/siddhu-py/my1stpycode.py", line 4, in add_numbers print(a + b) TypeError: unsupported operand type(s) for +: 'int' and 'str'" ...

React Js Commands

React JS Commands and Useful purposes  To Install react app: npx create-react-app app-name To Install react app: npx create-react-app . To check npm version: npm --version Inside that directory, you can run several commands:   npm start     Starts the development server.   npm run build     Bundles the app into static files for production.   npm test     Starts the test runner.   npm run eject     Removes this tool and copies build dependencies, configuration files     and scripts into the app directory. If you do this, you can’t go back! node -v (To check node version) The latest recommended command to create a new React app is: npx create-react-app@latest my-app Replace my-app with your desired project name. This approach uses the latest version of Create React App and works if Node.js (version 14+) and npm (version 5.2+) are installed Modern Alternatives If you prefer a faster, lighter setup, many developers n...