What is the benefit of Laravel's query builder?

- 1 answer


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)

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




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.

source: stackoverflow.com