How to specify folder for laravel migrations? - laravel

I am trying to put laravel database migrations in sub folders, but not sure how to do this.

When creating new migrations and executing your migrations, you can pass in a path parameter through the command line interface to specify the directory it will use to create and run the migrations respectively.
php artisan make:migration create_users_table --path=/path/to/your/migration/directory
php artisan migrate --path=/path/to/your/migration/directory

In AppServiceProvider, find a boot method and add there following
$mainPath = database_path('migrations');
$directories = glob($mainPath . '/*' , GLOB_ONLYDIR);
$paths = array_merge([$mainPath], $directories);
$this->loadMigrationsFrom($paths);
Now you can use php artisan migrate and also php artisan migrate:back when having migrations in sub folders

If you eager to do this, here is a quick solution at nscreed/laravel-migration-paths
pakcage. Very simple and easy to use.
During the periodical development phase the migrations folder may become very large. It is very helpful if we can organize the content of the migration folders. This library helps to organize migration files in different folders. Even, if your organize your existing files it will works as well.
Hope, it will help you.

You might want to take a look at this package, it's not exactly what you are looking for but it is an alternative to sorting and organizing sections of your application including controller, migrations, models, views and others.
Nwidart's Laravel Modules for modular application development with Laravel,
It helps to organize your application into modules, and so, you can create migrations in each module, and since the modules are in separate folders it helps fix this issue someway.

Related

php artisan make:controller is not defined

I am using Laravel Framework 6.20.44, which I can tell from php artisan -V
It comes part of OctoberCMS, which I just updated.
I'm jealous because the documentation for Laravel 6.x, and some many other resources, show that php artisan come with a make command, derived into so many subcommands.
I would like to enjoy a command like php artisan make:controller API/TopicController --api.
I can't, because my version of artisan returns:
Command "make:controller" is not defined.
Did you mean one of these?
create:controller
make:migration
It looks like I have an older version of artisan, heavily coupled to October.
I say coupled because I can spot commands such as october: when I list available artisan commands.
No luck with create:controller either, as --api is not implemented in my version...
My question: how do I update artisan so that I have access to the make command, according to the doco?
October CMS is developed in Laravel and it's not framework. It is CMS.
So, being CMS it has to maintain its directory structure with predefined paths. CMS is driven by plugins and themes. So you can only add/modify plugins or theme files.
So now if you try to add a controller where do it will add? it cants add to its system files OR in app/controllers/. To prevent this the authors of CMS have removed/overrideLaravel native commands to avoid creating the unexpected directory structure or files in core folders.
So basically you need to follow CMS rules and its command to maintain the directory structure and file structure of CMS. It's important otherwise CMS will not work.
php artisan create:controller <Author>:<PluginName> <ControllerName>
this is the exact definition: Create the controller in this plugin by this name. So it can work with CMS core logic.
Check some tutorials to understand how October CMS works:
How to install OctoberCMS
themes
How to edit OctoberCMS
themes
How to install OctoberCMS
plugins
Essential Plugins for
OctoberCMS
If any doubts please comment.

Laravel Migrations Scripts

I would like to know is there any feature in laravel to generate migration scripts dynamically ( I mean scripts not migration files) like in Doctrine?
I have researched in the internet but didn't find a solution.
In Eloquent I generated migration files and wrote migration scripts manully. But in Doctrine I can generate migration script from entities (models) by issuing command migrations:diff, migrations:migrate etc. Thanks in advance.
You never define any of your table's fields in your Model class, so Laravel doesn't even know where to get the diff in your database.
But if your database is already there, check this question: how to create migration from existing database in laravel

How to generate completely new migration in laravel 5.2

I am new to Laravel and was working on one project, which was 90% completed, and now project is 100% completed.
The project has old Migration files. I have made so many modifications in the tables (added/deleted columns) and/or added new tables in the database, now how do I update/create the Migration for that modifications? Because I don't remember where did I made the changes in the tables.
Do I have to use artisan command to create new migration for users table and all other tables same like this? php artisan make:migration create_users_table --create=users or there is any another way?
I have read the documentation but don't get how to do it.
Please correct me if I have made any mistake, because I don't know how to ask this question.
We user migration and tinker to Not use phpmyadmin , so if you have altered your tables in any way in phpmyadmin , you are all set , no need to do anything.
If you dont want to use phpmyadmin you can use migration and then start writing the sql code in there ( altering tables , etc)
And as we know the migration php files are located in your laravelfolder/database/migration . you can edit them there and then run php artisan migration and it will go throw all of them and make the changes in phpmyadmin.
Hope im clear enough :D

Can Laravel generate all mvc skeleton out of an existing table like that of cakephp's command cake bake all

I found laravel very interesting. But I'm wondering if there's any artisan command to generate all MVC skeleton files provided a database table all at ones. And how about separate generation of especially the model, given the table structure? or is there any alternative way to do the code generating?
You can create a migration file from the table via the package below.
https://github.com/Xethron/migrations-generator
Install it via : composer require --dev "xethron/migrations-generator"
Follow the package instructions, After connecting you application with the database simply run php artisan migrate:generate.
You will see number of migrations created via console message, You can also check your database/migrations folder for confirmation.
I didn't find how to do that, so I created my own code in order to create the models:
https://github.com/Triun/laravel-model-base
It is not super documented, but you can actually write your own addons in order to add custom business behaviors or include your own interfaces or traits to your models, with your own code.
And of course, reports and contributions are more than welcome.
I hope this helps.

lumen doesn't use the right database directory on migration

in Lumen when i try to create a database migration the artisan try to store the migration file on my-project/database folder which causes an error because the migration folder in Lumen located under my-project/model/migration. so why artisan doesn't use the Lumen migration folder.
As you have stated that lumen tries to store the migrations inside the database/migrations and it is the intended behaviour and is by the design.
If you want the proof it is here:
https://lumen.laravel.com/docs/5.4/database#migrations
It has already stated that it uses laravel type of migration behaviour.
So, you must try to maintain the structure if you want to create and run migration using default standard, which would be great if in near future you want to migrate to laravel from lumen.
Also you have stated that lumen's migration is inside model. It was never there by the design. You might have changed the folder structure of the migrations.
Want the proof :
https://github.com/laravel/lumen/tree/master/database/migrations
https://github.com/laravel/lumen/tree/5.0/database/migrations
https://github.com/laravel/lumen/tree/5.2/database/migrations
https://github.com/laravel/lumen/tree/5.3/database/migrations
Hope it clears your confusions.

Resources