What is the benefit of Laravel's query builder?

五迷三道 提交于 2019-12-10 17:37:30

问题


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.

Example:

$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();

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


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/34418117/what-is-the-benefit-of-laravels-query-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!