在Laravel中使用迁移和模型类有什么好处? [英] What are the benefits of using migrations and model classes in Laravel?

查看:76
本文介绍了在Laravel中使用迁移和模型类有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迁移(php artisan迁移)

Migrate (php artisan migrate)

我正在为我的应用程序使用laravel.我读过laravel doc,他们说:如果您不得不告诉队友将一列手动添加到他们的本地数据库架构中,那么您将面临数据库迁移所解决的问题."

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."

但是我正在与2位团队成员一起工作,我们使用的是实时数据库服务器.我更改的所有内容都进入了实时数据库,并且全部找到了.如果我使用实时数据库,是否应该像DOC所说的那样使用数据库迁移,对本地数据库有用?

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?

模型类

我仅将模型类用于Auth(用于表用户和角色).对于我所有的查询结果,我都使用原始sql查询,例如:

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);

我正在使用此命令(sql raw),因为我可以在sqlyog中检查我的复杂查询,然后粘贴到我的控制器函数中.我不确定是否可以在使用模型类的雄辩查询中进行一些复杂的查询.

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.

我的问题是我应该始终使用实时数据库还是本地数据库进行迁移?我是否应该对所有表始终使用Model类?即使我处于生产模式,我是否也应该使用迁移(我的意思是当应用程序上线时是否需要从客户端进行一些修改)?迁移可以实时进行吗?

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?

我做错什么了吗?我该如何正确使用laravel功能?

Am i doing something wrong? How can i proper use of laravel features?

推荐答案

简短答案:

我应该始终使用实时迁移,无论是实时数据库还是本地数据库

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.

我应该对所有表始终使用Model类吗?

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,绝对是-作为第一个答案

IMO, absolutely yes - as the 1st answer

迁移可以实时进行吗?

Does migration work on live?

是的,它应该适用于任何环境

Yes, it should work for any environment

这篇关于在Laravel中使用迁移和模型类有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆