问题
Migrate (php artisan migrate)
I am using laravel for my app. I read laravel doc in which they say "If you have ever had to tell a teammate to manually add a column to their local database schema, you've faced the problem that database migrations solve."
But i am working with my 2 team member and we use a live db server. Everything i changes goes to live db and all find that. If i use live db, should i use db migtration as DOC says it is useful for local db?
Model Class
I used model class only for Auth (for the table users and roles). For all of my query result i used raw sql query like:
$hal = DB::select('SELECT TotalForeignTruck,
(( UNIX_TIMESTAMP(truck_receivedate)- UNIX_TIMESTAMP(truck_entrydate))/60/60/24) AS HoltageDay
FROM(
SELECT
(SELECT truck_entry_regs.truckentry_datetime FROM truck_entry_regs WHERE truck_entry_regs.manf_id=m.id ORDER BY truck_entry_regs.id ASC LIMIT 1)AS truck_entrydate,
(SELECT truck_entry_regs.receive_datetime FROM truck_entry_regs WHERE truck_entry_regs.manf_id=m.id ORDER BY truck_entry_regs.id DESC LIMIT 1)AS truck_receivedate,
(SELECT COUNT(truck_entry_regs.id) FROM truck_entry_regs WHERE truck_entry_regs.manf_id=m.id )AS TotalForeignTruck
FROM manifests m
WHERE m.manifest=? ) t', [$r->mani_No]);
return json_encode($hal);
I am using this (sql raw) because i can check my complex query in sqlyog then paste in my controller function. I am not sure whether some complex query can be made in eloquent query that uses model class.
My question is should i use always migration whether live or local DB? Should i always use Model class for all of my tables? Should i use migration even if i am on production mode (i mean if need some modification from my client when app on live)? Does migration work on live?
Am i doing something wrong? How can i proper use of laravel features?
回答1:
Short answers:
should i use always migration whether live or local DB
Yes, migration will help to sync DB changes among multiple environments. Meaning each change will be updated to all env perfectly and correctly. And migration will help to safely rollback DB if there is any error.
Should i always use Model class for all of my tables
It's up to you. You have to know exactly what you are doing with the migration. Model class will help you easy to implement and reduce human issues.
Should i use migration even if i am on production mode (i mean if need some modification from my client when app on live)
IMO, absolutely yes - as the 1st answer
Does migration work on live?
Yes, it should work for any environment
回答2:
First, doing development on a live production server is NOT recommended, by anyone, and you really ought to stop that immediately.
Secondly, migrations help with that by easily keeping all of your DB instances in sync.
Another huge advantage is that your SQL changes are documented, over time, in source code and probably backed up in revision control (git, etc.).
As for Eloquent, you don't have to use it. I don't, by choice. But you really should utilize what are called Data Transfer Objects (DTOs) and create your own Database Access Layer (DBAL) using the raw SQL. Just don't have it strewn about your app willy nilly.
回答3:
Its not recommended to develop on production server.
My question is should i use always migration whether live or local DB?
yes.Its not required but its recommended to use migration in both and should be same migration files that would make source control and management easier and could test compatibility issues. If you keep migrations that way you can just git clone and start from where it is.
Should i always use Model class for all of my tables?
Not all tables are required to have models, but its good to have model for each object entities like tables(users,sessions,addresses,etc) other than pivots(many to many intermediate table. eg: like user_role,role_permission). Some cases it's good to create models for pivots as well, suppose if you want to use it as an entity(eg: can create a model named Subscription for customer_movie pivot).
Should i use migration even if i am on production mode (i mean if need some modification from my client when app on live)?
yes. as migrations are used for management of incremental, reversible changes to your db its always recommended to keep same migrations in both development and production environments. when you run migrate command it only runs the new migrations(up method) as incremental and when you run rollback command it reverses the migration (down method) in order last ran migration first. so you can bring incremental updates easily. if anything goes wrong could revert back to previous condition.(only if the down method is written to reverse whats done on up method). to create tables you can use Schema::create and to modify you can you Schema::table.
Does migration work on live?
yes. It works on both development and production. When you set environment to production when you run php artisan migrate it will prompt for each to confirm for safety. If you want to just run only the migrations that are required, you can do that too.
回答4:
Ad Migrations - my experience.
When I started with Laravel, I didn't get the full benefit of migrations, so I managed my database manually using phpMyAdmin. It is working well on localhost. However, I developed my project on localhost, than tested it on the stage server and then moved it to the production. Suddenly, I had 3 databases to manage and they were supposed to be the same.
What happened was, that I forgot to update the live DB and after pushing the latest version to the production, the web crashed at some point due to the DB differences, e.g. data type/ foreign keys/constraints. And it is a nightmare to localize the issue.
Migrations let you keep track of the changes which definitely will occur during development. E.g. adding new column, changing data types, changing foreign keys etc.
If you work in a team, there might be more local versions of your app. The DB management will become extremely difficult without migrations.
来源:https://stackoverflow.com/questions/46272081/what-are-the-benefits-of-using-migrations-and-model-classes-in-laravel