Convert Laravel Query builder to Eloquent builder
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
From Query Builder to Eloquent: Mastering the Transition for Cleaner Laravel Development
As developers working within the Laravel ecosystem, we often find ourselves navigating the space between raw database interaction and the powerful abstraction offered by Eloquent ORM. A common hurdle arises when moving from the raw DB facade (Query Builder) to using Eloquent Models. Specifically, there is a fundamental shift in mindset required: moving from asking "What SQL do I need?" to asking "What data do my objects need?"
This post dives into exactly how you can effectively convert your Laravel Query Builder operations into idiomatic Eloquent Builder calls, ensuring your code is cleaner, more maintainable, and leverages the full power of object-oriented programming.
Understanding the Paradigm Shift
The core difference lies in abstraction. The Laravel Query Builder (DB facade) deals directly with SQL strings and result arrays. While powerful for highly specific, complex joins that might not map cleanly to a single model relationship, it requires you to manually handle hydration (turning raw rows into meaningful data).
Eloquent, on the other hand, abstracts the database layer entirely. When you use an Eloquent Model, you are operating on objects. This means methods like where(), with(), and select() operate on the model context, automatically handling object mapping and relationship loading.
The reason you might feel the need to convert is that returning an array from methods like DB::get() requires extra steps if you want to immediately treat that result as a collection of models. Eloquent naturally returns Laravel Collections when querying models, which is far more useful in modern PHP applications.
The Conversion Process: Query Builder to Eloquent Builder
The conversion isn't about replacing every single query; it’s about choosing the right tool for the job. For standard CRUD operations and data retrieval based on model structure, Eloquent should be your default choice.
Here is a comparison illustrating the transition you are aiming for:
Raw Query Builder Approach:
$query_builder = DB::table('products')
->where('price', '>', 50)
->select('name', 'price');
$results = $query_builder->get(); // Returns a standard PHP array of objects/arrays
Eloquent Builder Approach:
use App\Models\Product;
// Eloquent automatically handles the table reference (products)
// and returns an Eloquent Collection.
$results = Product::where('price', '>', 50)
->select('name', 'price')
->get(); // Returns a Laravel Collection of Product models
Notice the difference: the Eloquent approach immediately gives you a Collection of Product objects, ready for immediate use in other model methods (like iterating or casting). This is inherently more useful than handling a raw array. As noted on laravelcompany.com, embracing ORM principles leads to code that is much easier for new team members to understand and maintain.
Best Practices: Leveraging Eloquent’s Power
When transitioning, focus on using Eloquent's strengths rather than trying to force Query Builder syntax into an Eloquent context.
1. Use Model Methods for Filtering
Instead of chaining complex DB::where clauses manually, define these conditions directly on the model itself. This keeps the data logic tightly coupled with the entity it represents.
2. Master Eager Loading
The true power of Eloquent comes from relationships. If your query needs to fetch related data (e.g., fetching all users along with their permissions), using with() is vastly superior to manually writing complex LEFT JOIN statements in the Query Builder. This pattern significantly reduces boilerplate code and improves readability, which aligns perfectly with the philosophy emphasized by Laravel development practices on laravelcompany.com.
Conclusion
Ultimately, while the Query Builder remains a necessary tool for deep SQL optimization, Eloquent is the preferred path for the vast majority of application logic. By making the conscious decision to use Eloquent for data retrieval, you shift from managing raw database results to managing meaningful domain objects. Embrace this transition; it will result in more readable, safer, and significantly more powerful Laravel applications.