skip to Main Content

I am working on Laravel^9.19" project with php^8.1, I created a Modules folder and then I created a migration files inside, my structure is like this :

--app
--Modules
----user
------Database
--------Migrations
----------2023_07_19_220540_create_users_table.php

and inside my AppServiceProvider :

public function register()
{
    //
    $this->loadMigrationsFrom(base_path('Modules/UserManagement/Database/Migrations'));
}

and inside my composer.json

"autoload": {
    "psr-4": {
        "App\": "app/",
        "Modules\": "Modules/",
    }
},

the problem is when I am trying to execute php artisan migrate
it gave me nothing to migrate because it is not scanning any folder other than app/database
I search online so much but I couldn’t find a solution, kindly advice with the solution in this case

2

Answers


  1. To run migrations for tables inside modules in Laravel, you can follow these steps:
    Create a new migration for the module table using the command php artisan make:migration create_module_table –create=module. Replace "module" with the name of your module.

    e.g. php artisan migrate –path=modules/ModuleName/database/migrations

    In the migration file, define the schema for the module table using the Schema facade. For example, to create a table with "id" and "name" columns, you can use the following code:

    use IlluminateSupportFacadesSchema;  
    use IlluminateDatabaseSchemaBlueprint;  
    use IlluminateDatabaseMigrationsMigration;  
      
    class CreateModuleTable extends Migration  
    {  
        public function up()  
        {  
            Schema::create('module', function (Blueprint $table) {  
                $table->bigIncrements('id');  
                $table->string('name');  
                $table->timestamps();  
            });  
        }  
      
        public function down()  
        {  
            Schema::dropIfExists('module');  
        }  
    }  
    
    1. Run the migration using the command php artisan migrate. This will create the module table in the database.
      If you have more migrations for other tables in the module, repeat steps 1-3 for each table.

    Note: If you’re using a modular structure in Laravel, you may need to update the migrations table to include the module name. You can do this by modifying the getConnection method in your module’s service provider.

    Login or Signup to reply.
    1. Create a migration inside the module:
      Inside your module directory, create a new migration file using the make:migration Artisan command. For example, if your module is named "MyModule," run the following command:
    php artisan make:migration create_my_module_table --path=modules/MyModule/Database/Migrations
    
    1. Define the migration schema:
      Open the newly created migration file inside the modules/MyModule/Database/Migrations directory and define the table schema inside the up method:
    <?php
    
    use IlluminateDatabaseMigrationsMigration;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateSupportFacadesSchema;
    
    class CreateMyModuleTable extends Migration
    {
        public function up()
        {
            Schema::create('my_module_table', function (Blueprint $table) {
                $table->id();
                // Define your table columns here
                $table->string('name');
                $table->timestamps();
            });
        }
    
        public function down()
        {
            Schema::dropIfExists('my_module_table');
        }
    }
    
    1. Run the migration:
      To run the migration for the tables inside the module, use the migrate Artisan command with the --path option to specify the path to the module’s migration directory:
    php artisan migrate --path=modules/MyModule/Database/Migrations
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search