1

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

miken32
  • 42,008
  • 16
  • 111
  • 154
  • 1
    You are trying to load files from `Modules/UserManagement/Database/Migrations/` but according to your post you have files in `Modules/user/Database/Migrations/` – miken32 Jul 19 '23 at 23:28
  • You have to simulate a composer package, have a literal package but local – matiaslauriti Jul 19 '23 at 23:30

2 Answers2

-2
  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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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
Dinh Luong
  • 127
  • 3
  • 15
-3

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 Illuminate\Support\Facades\Schema;  
use Illuminate\Database\Schema\Blueprint;  
use Illuminate\Database\Migrations\Migration;  
  
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.

  • so if in the future there are 10 modules, you would have to run `php artisan migrate --path=modules/ModuleName/database/migrations` 10 times? – kris gjika Jul 20 '23 at 08:21
  • Welcome back to Stack Overflow, Software Engineer. It looks like it's been a while since you've posted and may not be aware of the latest policies since your answer appears likely to have been entirely or partially written by AI (e.g., ChatGPT). As a heads-up, [posting of AI-generated content is not permitted on Stack Overflow](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. We do hope you'll stick around and continue to be a valuable part of our community by posting *your own* quality content. Thanks! – NotTheDr01ds Jul 22 '23 at 12:41
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Jul 22 '23 at 12:41