Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Creating custom HTML plugins for your website

    March 2, 2025

    WooCommerce Deposits and Partial Payments WordPress Plugin.

    February 26, 2025

    How to display API data on WordPress frontend using shortcode?

    February 25, 2025
    Facebook X (Twitter) Instagram
    • Privacy Policy
    • Disclaimer
    • Contact Us
    Facebook X (Twitter) LinkedIn Instagram YouTube
    Geeks Lab TechnologyGeeks Lab Technology
    Demo
    • Home
    • HTML/ CSS
      1. HTML
      2. CSS
      3. Bootstrap
      4. Tailwind
      5. Sass
      6. View All

      Creating custom HTML plugins for your website

      March 2, 2025

      HTML to Laravel Blade with CSS Assets

      October 23, 2024

      A Beginner’s Guide to PHP

      October 16, 2024

      How Convert a static HTML/CSS site to React App

      October 10, 2024

      Box Shadow vs Drop Shadow for Image in HTML and CSS

      October 10, 2024

      How to Set Up Tailwind CSS with Vite and React: A Step-by-Step Guide

      October 2, 2024

      Getting Started with Tailwind CSS: A Beginner’s Guide

      October 2, 2024

      Creating Header / Navbar with Bootstrap

      October 1, 2024

      Learn Bootstrap

      September 30, 2024

      How to Set Up Tailwind CSS with Vite and React: A Step-by-Step Guide

      October 2, 2024

      Getting Started with Tailwind CSS: A Beginner’s Guide

      October 2, 2024

      Minifing Sass CSS For Your Website Speed

      October 9, 2024

      Mastering in Sass: A Beginner’s Guide Improvements to Your CSS Work process

      October 9, 2024

      Creating custom HTML plugins for your website

      March 2, 2025

      How to use JQuery Swipe Slider?

      February 17, 2025

      How to Use OWL Carousel?

      February 17, 2025

      How to Integrate jQuery Magnifying Popup?

      February 17, 2025
    • WordPress
      1. WordPress CMS
      2. WordPress themes
      3. WordPress Plugins
      4. WooCommerce
      5. Tutorials
      6. WP Page Builders
      7. WP Speed Optimize
      8. View All

      WordPress Security in Easy Steps (No Coding)

      February 13, 2025

      How to add Custom Class and ID in Contact Form 7

      December 30, 2024

      How to Generate Automatic WooCommerce QR Code

      December 23, 2024

      Translate WordPress Website Using GTranslate Plugins

      December 22, 2024

      WordPress Security in Easy Steps (No Coding)

      February 13, 2025

      All About WordPress Child Theme

      February 11, 2025

      How to Fix 500 Internal Server Error in WordPress Website

      December 18, 2024

      How to fix the WordPress White Screen of Death (WSOD) Issue on Website

      December 18, 2024

      WooCommerce Deposits and Partial Payments WordPress Plugin.

      February 26, 2025

      WordPress Security in Easy Steps (No Coding)

      February 13, 2025

      How to set up the country flag and phone numbers in contact form 7?

      January 30, 2025

      How To Check WordPress Website Health And Improve It?

      January 28, 2025

      WooCommerce Deposits and Partial Payments WordPress Plugin.

      February 26, 2025

      How to Generate Automatic WooCommerce QR Code

      December 23, 2024

      How to setup and Configure WP Mail SMTP Plugin in WordPress

      December 15, 2024

      WooCommerce Tips: How to Make users purchase only one product per order

      October 23, 2024

      WooCommerce Deposits and Partial Payments WordPress Plugin.

      February 26, 2025

      How to display API data on WordPress frontend using shortcode?

      February 25, 2025

      Create Tables in Elementor

      January 7, 2025

      How to add Custom Class and ID in Contact Form 7

      December 30, 2024

      How to Customize the “Add to Cart” Button Text in WooCommerce

      October 15, 2024

      Best WordPress Page Builders: Enhance Your Website with Ease

      October 9, 2024

      Elementor Page Builder Loading Problem

      October 7, 2024

      How to Fix 500 Internal Server Error in WordPress Website

      December 18, 2024

      How to Optimize and Compress Images in WordPress

      October 14, 2024

      The Ultimate Popular WordPress Speed Optimization Plugins – 2025

      October 9, 2024

      WordPress Lite Speed Cache Plugin For Speed Optimization

      October 9, 2024

      WooCommerce Deposits and Partial Payments WordPress Plugin.

      February 26, 2025

      How to display API data on WordPress frontend using shortcode?

      February 25, 2025

      WordPress Security in Easy Steps (No Coding)

      February 13, 2025

      All About WordPress Child Theme

      February 11, 2025
    • PHP
      1. PHP Tutorials
      2. MySql
      3. View All

      PHP Variables in Detail

      October 17, 2024

      A Beginner’s Guide to PHP

      October 16, 2024

      Multi-Level Menu Like Primary Category and Sub Category Menu using PHP MySQL

      January 28, 2025

      How to connect PHP and MySQL Database?

      January 23, 2025

      How to display API data on WordPress frontend using shortcode?

      February 25, 2025

      Multi-Level Menu Like Primary Category and Sub Category Menu using PHP MySQL

      January 28, 2025

      How to connect PHP and MySQL Database?

      January 23, 2025

      PHP Variables in Detail

      October 17, 2024
    • Laravel
      1. Laravel Beginners
      2. Laravel Tutorials
      3. View All

      Laravel Eloquent Relationships: An Advanced Guide

      November 19, 2024

      Laravel 11 Send Email Via Notification

      November 14, 2024

      How to Add Toastr Notification in Laravel 11?

      November 14, 2024

      Laravel 10 Generate Test or Dummy Data using Factory Tinker

      November 13, 2024

      Clear Laravel cache in different ways

      October 21, 2024

      Laravel Eloquent Relationships: An Advanced Guide

      November 19, 2024

      Laravel 11 Send Email Via Notification

      November 14, 2024

      How to Add Toastr Notification in Laravel 11?

      November 14, 2024

      Laravel 10 Generate Test or Dummy Data using Factory Tinker

      November 13, 2024
    • JavaScript
      1. React Js
      2. View All

      Password and Confirm Password Validation in React JS

      November 12, 2024

      Username and Password Validation in React JS

      November 11, 2024

      How Convert a static HTML/CSS site to React App

      October 10, 2024

      How to Set Up Tailwind CSS with Vite and React: A Step-by-Step Guide

      October 2, 2024

      Creating custom HTML plugins for your website

      March 2, 2025

      How to use JQuery Swipe Slider?

      February 17, 2025

      How to Use OWL Carousel?

      February 17, 2025

      How to Integrate jQuery Magnifying Popup?

      February 17, 2025
    • Contact
    Geeks Lab TechnologyGeeks Lab Technology
    Home»Editors Choice»DB structure migrations env config in Laravel
    Editors Choice

    DB structure migrations env config in Laravel

    GeeksLab TechnologyBy GeeksLab TechnologyOctober 22, 2024Updated:October 22, 2024No Comments3 Mins Read8 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Laravel uses database structure migrations to efficiently handle changes to your database schema in a version-controlled environment. Migrations are defined as PHP classes, and Laravel’s migration system executes them to uniformly modify your database schema across many environments, including development, staging, and production.

    Important steps for configuring your environment and using migrations in Laravel:

    1. Environment Configuration (.env file)

    First, use the.env file to configure your environment’s database connection. Update the following keys based on your database configuration:

    DB_CONNECTION=mysql       
    DB_HOST=127.0.0.1         
    DB_PORT=3306              
    DB_DATABASE=your_database_name  
    DB_USERNAME=your_username       
    DB_PASSWORD=your_password       
    

    If you have numerous environments (e.g., development and production), you can use separate.env files or configure various environment variables on each server.

    2. Creating Migrations

    Now generate a new migration file  by using the Artisan command:

    php artisan make:migration create_users_table
    

    This will generate a migration file in the database/migrations directory with a timestamp in the filename, ensuring that migrations are properly ordered. The file will have a class with up() and down() methods.

    • The up() method defines the modifications to be made (for example, establishing a table or adding columns).
    • The down() method specifies how to reverse the modifications (for example, lowering the table or removing columns).
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateUsersTable extends Migration
    {
        public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
        }
    
        public function down()
        {
            Schema::dropIfExists('users');
        }
    }
    

    3. Running Migrations

    To run all outstanding migrations (that have not yet been run), run the following Artisan command:

    php artisan migrate
    

    Laravel will utilize the connection information in your.env file to connect to the given database and perform the migration.

    4. Rolling Back Migrations

    To undo the latest set of migrations, run the following command:

    php artisan migrate:rollback
    

    You can also specify how many batches to roll back by using the –step option:

    php artisan migrate:rollback --step=1
    

    To roll back and rerun all migrations:

    php artisan migrate:refresh
    

    To reset all migrations, drop all tables, and run them again:

    php artisan migrate:reset
    
    php artisan migrate
    

    5. Seeding Data

    Seeders are useful for populating the database with test data. You can create a seeder with the Artisan command:

    php artisan make:seeder UsersTableSeeder
    

    Once established, you can specify how to seed data using the run() method. For example, seeding the users’ table:

    use Illuminate\Database\Seeder;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Hash;
    
    class UsersTableSeeder extends Seeder
    {
        public function run()
        {
            DB::table('users')->insert([
                'name' => 'John Doe',
                'email' => 'john@example.com',
                'password' => Hash::make('password'),
            ]);
        }
    }
    

    You can run all seeders using:

    php artisan db:seed
    

    Or, run a specific seeder:

    php artisan db:seed --class=UsersTableSeeder
    

     6.Configuring Migrations for Different Environments

    In a multi-environment arrangement (local, production, etc.), you may choose to configure separate database connections for each environment. The Laravel configuration system allows you to configure this in the config/database.php file.

    For example, you could have a MySQL connection for development and a PostgreSQL connection for production. Your .env files would vary by environment:

    .env (Development)

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=dev_db
    DB_USERNAME=root
    DB_PASSWORD=root_password
    

    .env.production (Production)

    DB_CONNECTION=pgsql
    DB_HOST=prod-db.example.com
    DB_PORT=5432
    DB_DATABASE=prod_db
    DB_USERNAME=prod_user
    DB_PASSWORD=prod_password
    

    By following these instructions, you can ensure that Laravel manages your database schema consistently across several settings.

    db migrations db structure Laravel
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    GeeksLab Technology
    • Website

    Related Posts

    Creating custom HTML plugins for your website

    March 2, 2025

    WooCommerce Deposits and Partial Payments WordPress Plugin.

    February 26, 2025

    How to display API data on WordPress frontend using shortcode?

    February 25, 2025

    How to use JQuery Swipe Slider?

    February 17, 2025

    How to Use OWL Carousel?

    February 17, 2025

    How to Integrate jQuery Magnifying Popup?

    February 17, 2025
    Leave A Reply Cancel Reply

    Demo
    Top Posts

    How Convert a static HTML/CSS site to React App

    October 10, 202450 Views

    How to Customize the “Add to Cart” Button Text in WooCommerce

    October 15, 202423 Views

    How to Set Up Tailwind CSS with Vite and React: A Step-by-Step Guide

    October 2, 202423 Views
    Don't Miss

    Creating custom HTML plugins for your website

    March 2, 2025

    Custom HTML plugins on your websites improve its overall functionalities and user experience. Here’s are…

    WooCommerce Deposits and Partial Payments WordPress Plugin.

    February 26, 2025

    How to display API data on WordPress frontend using shortcode?

    February 25, 2025

    How to use JQuery Swipe Slider?

    February 17, 2025
    Stay In Touch
    • Facebook
    • YouTube
    • TikTok
    • WhatsApp
    • Twitter
    • Instagram
    Latest Reviews
    Demo
    Geeks Lab Technology
    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    • About
    • Privacy Policy
    • Disclaimer
    • Contact Us
    © All Rights Reserved Geekslabtechnology 2024. Designed by Geekslab.

    Type above and press Enter to search. Press Esc to cancel.