Can One Modify the Templates Created by Artisan Migrate Command

Can one modify the templates created by artisan migrate command?

I don't think you can, because Laravel takes migrations from the vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs folder and you cannot change that, but you have some options:

1) Create your own artisan command migrate:makemyown.

2) Use Jeffrey Way's Laravel Generators. They let you create your migrations by doing:

php artisan generate:migration create_posts_table --fields="title:string, description:text"

If you just have some fields you need to start with and not something more specific than that, it works really fine.

3) Edit Laravel stubs, but the problem is that as soon as you composer update they might get overwritten by Composer.

How to modify migrations and migrate it without loosing data?


Answer

If you have already created a database table and realise you need to add more into it you can simply run: php artisan make:migration add_field_to_table_name --table=tableName

You then go into that file and create the new field. Once done simply run php artisan migrate and it'll add the new field into the desired table without causing any data loss.


Seeders

On another note, I would strongly suggest looking into seeders. This way when you're creating a project you can always refresh your migrations (wipe your database and re-migrate your tables) and then re-input the data using seeders.

How Laravel Generate Model Migration Controller all command related files?

All the generated stuff in Laravel use templates

If you run artisan command in your console, you can observe that exists a section called stub, and the only command in this section is php artisan stub:publish.

If you run that command it will generate a new folder inn your app root folder called stubs with a bunch of files inside all with extension .stub.

You can open those files and modify then or customize them as needed. From now on this folder will be the place from where your Laravel app will read the template for making all kind of stuff that artisan usually does.

This templates are included with every Laravel installation and is totally optional publish them or not. In fact there are many packages dedicated to make custom Controllers or Models like this one from Spatie

The internals above this generators
Laravel has two kernels,

  1. The first one in app/Console/kernel
  2. The second one in app/Http/kernel

When you run artisan, Laravel Bootstrap the app, and run the Kernel console. This both Kernels has different purposes, really they function as separates apps.

About the specific generation of the above files, I mean different controllers, Models, migrations etc.. all that stuff related to models are generated by one Class.

class ModelMakeCommand extends GeneratorCommand{ .... }

Which is located under Illuminate\Foundation\Console namespace.

You can check the code of that class and see how the stubs files are used to generate the variety of commands only related to Models, but there are many more, like Policies, Events, Jobs etc...

I hope this helps and answer your question

Here you are more information about this subject from Laravel News

How do I change the automatically generated migration page in Laravel?

If you dig around in the source code you will find that:

  1. There's a file called create.stub in the framework.
  2. This file is used by the MigrationCreator to make migrations.

In principle you can do the following:

  1. Grab the built-in migration file and move it in another folder in your project (e.g. resouces/stubs probably) Note that you should copy the other stubs in that folder too even if you won't modify them.

  2. Then, override the default migration creator to use this file instead, this should work:

    class MyMigrationCreator extends MigrationCreator {
    protected function stubPath() {
    return base_path("resources"); //Or something valid
    }
    }

Then in your application service provider you can do:

$this->app->instance(MigrationCreator::class, resolve(MyMigrationCreator::class));

This will (hopefully) "trick" laravel into using your migration creator than the default one. However, creating tables is not something that happens so often to justify all this trouble.

Update: It should extend the migration creator.

Laravel's Artisan says nothing to migrate

That foo thing is just an example. Laravel will look for migrations to run in app/database/migrations on default. Try removing that --path parameter and see if it works.



Related Topics



Leave a reply



Submit