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,
- The first one in app/Console/kernel
- The second one in app/Http/kernel
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:
- There's a file called create.stub in the framework.
- This file is used by the
MigrationCreator
to make migrations.
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.
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
}
}
$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
Speeding Up Large Numbers of MySQL Updates and Inserts
Save Accents in MySQL Database
Codeigniter Active Record Not Equal
How to Retrieve Date from Table Cell Using PHPspreadsheet
How to Add Custom Fields to Woocommerce Registration Form
PHP MySQL Pagination with Random Ordering
PHP Explode Array Then Loop Through Values and Output to Variable
How to Force a Certain Tls Version in a PHP Stream Context for the Ssl:// Transport
Codeigniter 3 - Access Session from Outside Codeigniter Installation
Download the Contents of a Url in PHP Even If It Returns a 404
How Would You Transform a Pre-Existing Web App into a Multilingual One
Using Moodle Create Users and Enroll Them in Courses via SQL
Storing Leading Zeros of Integers in MySQL Database as Integer
Create a Joomla! Article Programmatically