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
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:
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.
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:
Open the newly created migration file inside the
modules/MyModule/Database/Migrations
directory and define the table schema inside theup
method: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: