Posted: 16-05-2017

Laravel Eloquent vs Query builder

Laravel Eloquent vs Query builder

One of the coolest features of Lavarel is Elequent ORM. But if you needs to query a model with his relation Query builder is the rigth option for you.

Consider this query:

Eloquent with Eager Loads

$countries = Country::with(['country_translations' => function ($query) {
                            $query->where('locale', '=', 'en');

query result
select * from countries select * from country_trasnslation
where locale='en' and id in (1, 2, 3, 4, 5, ............. 231)

I think to have more tha 200 values in the id in (1, 2, 3, 4, 5, ............. 231)  it's not a efficient solution.  


Query builder

$countries = DB::table('countries')->where('locale','en') 
->select('country_translations'.label as country','countries.iso_code as iso_code')
query result select country_translations.label as countrycountries.iso_code as iso_code
from countries
left join country_trasnslation on = country_translations.country_id
where locale='en'

You can found a very interesting article about this topics at the following link