Laravel的查询生成器有什么好处? [英] What is the benefit of Laravel's query builder?
问题描述
我正在学习使用Laravel 5,但是我处于查询生成器阶段,在看了第一段代码片段后,我认为如果某人具有基本的SQL知识,那真的没用.
I am learning to use Laravel 5, but I am at the query builder phase, and after looking at the first code snippets I think it is really useless when somebody has basic SQL knowledge.
示例:
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
如果程序员实际上至少具有有关SQL语言的基本知识,那么学习查询生成器语法的目的是什么?而且,将高级查询从一种干净的SQL语言转移到查询生成器功能看起来像一场噩梦...
What is the purpose of learning query builder syntax if the programmer actually has at least basic knowledge about the SQL language? Moreover, moving advanced queries from a clean SQL language to query builder functions looks like a nightmare...
推荐答案
除了它为您要切换数据库引擎时提供了一个接口(但是,老实说,您看到大型应用程序实际切换数据库引擎的次数是多少) ?),我看不出太多优势.当然,这是一种更口才的"和"Laravel"的处理方式-本身就足以卖给那里的框架纯粹主义者.
Other than it provides an interface for when you want to switch database engines (but to be honest, how many times have you seen a large application actually switch database engines?), I don't really see too many advantages. It is, of course, the more "Eloquent" and "Laravel" way of doing things - which by itself will be enough of a sell to the framework purists out there.
如果有的话,我认为使用查询生成器会使新开发人员的注册工作变得困难得多. SQL是几乎所有开发人员都知道的通用语言.如果您的团队中有一个对Laravel不太了解的新成员,那么他可能需要一天的时间来熟悉查询生成器.
If anything, I think using query builder would make onboarding a new developer that much harder. SQL is a universal language that almost all developers know. If you have a new member on your team who doesn't know Laravel well, chances are it'll take him a day to get up to speed with query builder.
我已经开始接受在Eloquent模型中使用关系进行简单的一个表联接,或使用引用表进行两个表联接.但是,如果需要两个以上的联接,那么您实际上应该写出自己的SQL或使用查询生成器.在这两种选择中,我个人更喜欢编写原始SQL.
I have come to accept to using relationships in my Eloquent models for simple one table joins, or two table joins using a reference table. But anything requiring more than two joins, you should really be writing out your own SQL or using query builder. And out of the two choices, I personally prefer writing raw SQL.
这篇关于Laravel的查询生成器有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!