Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GuntarV's avatar
Level 40

Simple SQL query vs Eloquent

To me, sometimes it is easier to write a simple sql query versus creating an eloquent call for the same result.

For example if I need to return just a company name and id form Company table, but the results will filtered where the condition is met ~3 joints deep.

Are there certain benefits using eloquent, is it safer, faster or because of some other benefits..? I think eloquent sometimes can be even slower, because it will be running multiple queries to get the same/simple results.

What do you guys think?

0 likes
7 replies
jlrdw's avatar

Eloquent has to convert to normal SQL at runtime.

Active record is strong at helping with relations.

GuntarV's avatar
Level 40

Hmmm... how many different queries would eloquent have to run for this query:

select distinct c.name Company, c.id from companies c join properties p on p.company_id=c.id join dairy_crops dc on dc.property_id=p.id join dairy_crop_varieties dcv on dcv.id=dc.dairy_crop_variety_id join crops on crops.id=dcv.crop_id where dc.status = 'current' and crops.name = 'alfalfa' and c.active and p.active

Also, I think it would be pretty lengthy to write this kind of query in eloquent.

Thank you for your feedback @jlrdw

jlrdw's avatar

There are two sides of eloquent.

  • An eloquent query
  • Eloquent relations

So the above could be an eloquent query. Just example, not your data, but an eloquent query looks like:

$quy = Powner::query()->leftJoin('dc_pets', 'dc_powners.ownerid', '=', 'dc_pets.ownerid')
                ->select('dc_powners.ownerid', 'dc_powners.oname')
                ->selectRaw('count(dc_pets.petid) as countOfPets')
                ->groupby('dc_powners.ownerid')
                ->orderby('dc_powners.oname')
                ->get();

Results basically give:

ownerid, oname, countOfPets

Like:

5|Bob|3
4|Greg|9
2|Rob|1

Eloquent has all query builder methods available.

But if your query works just use as is, see: https://laracasts.com/discuss/channels/laravel/sql-native-to-query-builder

1 like
piljac1's avatar

Eloquent is technically slower, because it has to build the actual SQL query, but I wouldn't say that the difference is noticeable (except if Eloquent is not used correctly in the context). For example, the following code example will run two separate SQL queries :

Book::with('author')->where('title', 'like', '%a%')->get();

The following one only runs one query and is multiple times faster than the one above :

DB::table('books')->join('authors', 'books.author_id', 'authors.id')->where('title', 'like', '%a%')->get();

An entirely raw query equivalent to the one above is performed practically (I can almost say equally) as fast as the one above :

DB::select("select * from books join authors on books.author_id = authors.id where title like '%a%'");

As for safety, Eloquent is safer than a raw SQL query because it uses PDO out of the box (which prevents SQL injection with parametered queries). You can still use bindings with raw SQL queries though.

1 like
piljac1's avatar

@jlrdw That's what I said in the next sentence :P I edited my post to avoid confusion.

1 like
jlrdw's avatar

@piljac1 yes it's always good to remind folks to do proper bindings. Sorry didn't see the edit.

1 like

Please or to participate in this conversation.