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', ...

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...

Reactjs jsx

ReactJS JSX   React is a modular, scalable, flexible, and popular front-end framework. What is JSX? JSX is a syntax extension for JavaScript. It was written to be used with React. JSX code looks a lot like HTML. What does “syntax extension” mean? In this case, it means that JSX is not valid JavaScript. Web browsers can’t read it! If a JavaScript file contains JSX code, then that file will have to be compiled. This means that before the file reaches a web browser, a JSX compiler will translate any JSX into regular JavaScript. Codecademy’s servers already have a JSX compiler installed, so you don’t have to worry about that for now. Eventually we’ll walk through how to set up a JSX compiler on your personal computer. JSX is a syntax extension for JavaScript which allows us to treat HTML as expressions. const h1 = <h1>Hello world</h1>; They can be stored in variables, objects, arrays, and more! Here’s an example of several JSX elements being stored in an object: const ...