I want shorter names for my migration files.
This project will not have tens of thousands of migrations, therefor 2019_11_05_191747_create_units_table.php is verbose.
I want to have clean migration file names like:
0001_create_users_table.php
0002_create_units_table.php
But the code in vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php is exploding on _ and not liking it.
Symfony\Component\Debug\Exception\FatalThrowableError : Class '' not found
How to get this done?
You can't because you should not change Laravel main core (for update compatibility reasons)
You should use the migrations as intended with "YYYY_MM_DD_HHMMSS_action_table_table_description.php" format.
Well, there is a solution, which would be to create a custom command and do what laravel migration does but with another file format and that's a lot of work for just the name of the migration files.
Related
I have several database migrations coded in Laravel that I do not want to apply just yet.
I do not want to rename these files or move them into some other folder. If any of the migrations had been applied previously, moving or renaming them breaks Laravel's ability to roll them back.
I do not want a means of applying only one specific migration at a time.
I want a means of marking the class that is a modification to the migration file itself, not some external marking or process (such as adding rows to the migration table).
Is there a programmatic solution for this?
The answer : Laravel 5 - skip migrations may be of help for you, instead of skipping , choose the migrations you want to do directly .
What I'm referring too exactly is the use of caloskao/migrate-specific
In a previous question, I asked about how to write a trigger and gave a decent example of one way to write it in a laravel 5.X migration file. (note: hard/raw coded)
Referenced Question:
https://www.stackoverflow.com/questions/39177303/laravel5-x-database-triggers-and-possible-best-practices
However, as the title states, does anyone know how to write a database routine/subroutine in a laravel migration file??
Or
Does anyone know of a clean way to implement using laravel's methods to create a migration file that will make subroutines or routines within a database?
My question is if it is possible to add all the fields directly to a new model via Eloquent.
I guess it would be something like
php artisan make:model MyModel --fields=?
However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.
Thanks in advance
If you mean table's column by fields then:
Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.
So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.
Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
You can checkout their documentation for more information. Best of luck.
It's not possible with make:model or make:migrations commands, but you can create your own console command and add this feature by yourself.
Also, take a look at source code of make:model and make:migration commands to get some ideas on how to do that.
it looks like only built in options are --migration and -m to include a migration with the model generation. L5.3 Docs
There does look like there is a package for L5.0, which looks like it would work in 5.*+. It is put out by Laracasts:
https://github.com/laracasts/Laravel-5-Generators-Extended
It also looks like you can make a custom solution as well:
https://laracasts.com/discuss/channels/tips/l5-artisan-command-makemodel
Hope that helps!
No options while creating a model,
This is my terminal output (laravel 5.3) while i check,
You don't need to mention fields while creating model.
Ex:- based on the rules you should keep the names as like below,
model name as User
table name as users
then the model automatically handle everything, you don't need to mention the table/fields name.
I was looking for the same thing myself, as I used to work like that in previous frameworks, but could not find it, or at least not as I wanted it, so I did my thing. You can check it out if you like:
https://github.com/Triun/laravel-model-base
It will read your database, and create the laravel eloquent models for you.
It is meant to be very flexible, so the configuration may be a little complex, and I guess that I didnt' catch up with the documentation, but you can ask me if you don't know how to make it do what you want.
Basically it has 4 customization levels:
By out of the box modificators, configurable by the config files.
Including interfaces and traits to your auto-generated models.
Create your own modificators. Classes where you receive the model skeleton before it is saved, so you can add or remove properties, methods, etc.
Generate the model base, but edit yourself the final model.
And, of course, suggestions and contributions are more than welcome.
I want to get some details about recommended work process for Yii. Imagine you already have some database and some model for it. And in one day you need to add a new field to the model. In Django, you can just modify models.py file and then run manage.py makemigrations && manage.py migrate - it will analyze changes, create migration file and apply the changes to the database. But what I should do in Yii?
I see only following way from the docs and manuals:
Create empty migration
Try to write necessary changes in Yii-migration syntax (it may be not so obvious for altering column and adding foreign keys, more difficult than just writing SQL queries).
Run yiic migrate
Generate Model code using Gii for new database structure and copy-paste new fields to your existing Model file.
From my point of view, it leads to lot of useless work by creating migration in addition to modifying Model. So, instead of just modifying model like in Django, I have to use strange migration syntax in Yii and then modify model manually. It it really the way it supposed to work? Isn't it possible to simplify it somehow?
I'm using below approach for like 5-6 month and its work perfect:
create new folder inside models folder name it entities.
generate all models you need using gii and
generate all models you need using gii
a) in model path field use new folder, "entities" instead of models folder
b) in model class field, add "Entity" as model name postfix
now in models folder, make new PHP class and named it for example "Gift" and extends it from "GiftEntity"
add new folder, "entities" in preload imported classes.
now, when you make new migration and change your models in db, use gii to regenerate your entity models "GiftEntity", and all your codes in extended model "Gift" are untouched.
I'm looking for ideas to properly handle my project's mysql table updates across environments. I've taken a look at the CI DB Forge class and I believe this might help me out a bit. My thoughts are to:
create a new file for each database install, table upgrade or change. The file would contain the raw mysql query to do each relevant task
run the upgrade scripts via hooks before any controllers are loaded
continue loading the project
Is this the correct thinking? This is pretty similar to how Magento handles database upgrades per extensions.
Sounds like you are looking for the Migrations class. This is a fairly new library, and the documentation at this moment is not too good in my opinion.
If you enable this library in application/config/migrations.php and load it, the it will create a database table called migrations. The workflow from there is the following:
Create a new file under application/migrations make sure you name it with sequentially numbered file name like 001_some_descriptive_name.php. The format is important, exactly 3 numbers and at least one _ after them.
In the new file create a class named after the file name, so the 001_some_descriptive_name.php should hold a class called Migration_Some_descriptive_name and extend the CI_Migration class. The class name casing is important, first Migration_ then one uppercase letter then lowercase.
Create a public up and a public down method inside the class
Inside the up method add your migration code, that changes the database. You can use the db forge library or just plain old $this->db->query() calls. Dbforge is more portable if you need to support multiple database systems its probably better to use that.
Inside the down method add the code that would reverse the effects of up. If up adds a column then down should drop that column, if up creates a table, down should drop that table and so on.
Once you finished your migration class bump the migration_version inside the migration config file.
Create a controller load the migration library and call $this->migration->current() this will check the version from the migration database table and run the migration classes up or down methods in order to reach the migration version in the config file you set at step 6. So for example if the database says you are at version 2, and the config says you should be on 5, then it will run the up method of the migrations with 003_..., 004_..., 005_.. filenames in order. If you set lower number then the current the down methods will be called. The database starts the counting from 0 so don't create a 000_... file.
If you feel adventurous you can create a hook that loads the migration class and runs the get_instance()->migration->latest() on every page load so every environment will auto update the db once you deploy a new migration class.