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.

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

Laravel Commands

Laravale commands #Check route list php artisan route:list #Check upload files links php artisan storage:link #Check database connected or not php artisan db #Make Request file php artisan make:request YourNameRequest #Make Controller #(In this statement you used -r -> resources and -m -> model. It will create CustomersController and Customers Model files) php artisan make:controller CustomersController -r -m Customers #Make Resource file php artisan make:resource CustomersResource #To check migration files status that those files are running or not with below commands php artisan migrate:status #To check if there is any pending migrate files to run #(also this command shows us the mysql query before running migration file) php artisan migrate --pretend #To make a database table (in this example Products name as taken) php artisan make:migration create_products_table #To create a Request file php artisan make:request StoreProductRequest php artisan make:request Up...

What is AngularJS?

AngularJs is nothing but an MVC style javascript framework for creating single page apps. It is a javascript framework or we can say  "A framework will build client applications in HTML, CSS and Javascript/Typescript" This should be added within HTML  <script> tag. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script> Angular Js mainly used to make a single page, data-driven application. As well as we can add additional content brought into the web page without the need to refresh. Before going to AngularJS, We should know the     HTML     CSS     JavaScript     Json (optional)  Benefits of using angular Js     Gives our applications a clean structure     Includes a lot of re-usable code     Makes our applications more testable      Using Angular makes your life easier!   Let's Start learning with ...