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

ashokfyn's avatar

Laravel Eloquent Model Vs Laravel Facades DB::

can you help me understand the difference between using Eloquent models (e.g., Model::find()) and DB facades (e.g., DB::table()) in Laravel? When should I choose one over the other, and what are the advantages of each approach?"

0 likes
3 replies
Tray2's avatar

They are just different types of syntactic suger, they both boil down to SQL in the end.

These two does the same thing.

Book::where('title', $title)->get();

DB::table('books')->where('title', $title)->get();

SELECT *
FROM books
WHERE title = ?
3 likes
JussiMannisto's avatar

They both offer a query builder, but Eloquent is a full-blown ORM with a lot more features. Among other things, it allows you to define relations between models. That allows you to do things like:

foreach($user->posts as $post)
	...

$user->posts()->create([
	'title' =>  'foo',
	'message' => 'bar'
]);

$user->posts()->withCount('likes')->get();

You can achieve the same with the DB facade, but you'll have to write a lot of joins or secondary queries to fill in data, while Eloquent does them for you. Eloquent also caches relations, so that calling $user->posts multiple times on the same model doesn't result in multiple database queries.

Eloquent also handles data mutation, casting, JSON serialization, etc. It's kind of a big topic, so I suggest you read about it here to see what it offers: https://laravel.com/docs/12.x/eloquent

The DB facade is a straight-up SQL query builder that knows nothing about models and their relations.

In short: use Eloquent if you can, DB if you have a good reason.

2 likes
kevinbui's avatar

Replies by @tray2 and @jussimannisto are spot on!

I am just verbose now. Eloquent has to be tied to a Model class. on the other hand, DB facade/builder doesn't have to be.

So the following statement will return a collection of Book objects.

Book::where('title', $title)->get();

And the below statement will return a collection of stdClass objects.

DB::table('books')->where('title', $title)->get();

I use Eloquent to construct database queries 99% of the time. I only use the DB facade when I got a bunch of aggregated results that cannot be tied to any Model/Table.

Eloquent and the DB facade are indeed tightly related. There are a lot of subtle differences and similarities.

If you are new to Laravel, I suggest watching Eloquent series, and those favorite classes of mine:

Illuminate\Database\Eloquent\Model
Illuminate\Database\Eloquent\Builder
Illuminate\Database\Query\Builder
1 like

Please or to participate in this conversation.