newQuery() in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Decoding Eloquent: `newQuery()` vs. Direct Model Instantiation in Laravel As developers working with Laravel and Eloquent, understanding how to chain methods effectively is crucial for writing clean, efficient, and maintainable code. Often, there are subtle differences between starting a query chain versus instantiating a model directly. This distinction might seem trivial at first glance, but it impacts how Eloquent interacts with the database, especially when dealing with complex filtering or scope application. Let's break down the two approaches presented and explore why one method is generally preferred in scenarios where you are building dynamic queries. ## The Two Approaches Compared We are comparing two ways to execute a sequence of operations on an Eloquent model: ### Approach 1: Using `newQuery()` for Query Building ```php $model = User::newQuery(); $model->published(1); $model->get(); ``` In this approach, we explicitly start with the static method `newQuery()`. This immediately tells Eloquent that we are initiating a **Query Builder** instance. The subsequent methods (`published()`, etc.) are then chained directly onto this query builder to construct the final SQL statement before execution via `get()`. ### Approach 2: Direct Instantiation and Chaining ```php $model = User; // Or $model = new User(); $model = $model->published(1); $model = $model->get(); ``` Here, we start by referencing the Model class itself. When you call methods on the model instance directly, you are operating on an established **Model Instance**. While this works for single record operations (like finding a specific ID), chaining complex filtering and selection might behave differently or be less intuitive when dealing with broader query construction. ## The Developer's Perspective: Why It Matters The fundamental difference lies in the object context you are manipulating: a **Query Builder** versus an **Eloquent Model Instance**. When you use `User::newQuery()`, you are explicitly telling Laravel, "I want to build a database query." This is the canonical way to start constructing SQL statements. This pattern aligns perfectly with how we interact with the underlying database layer in Laravel. For more complex data retrieval or filtering, this approach keeps your intent clear and leverages Eloquent’s powerful query capabilities. In contrast, directly instantiating `$model = User;` sets up a model object. While you *can* chain methods onto it, for operations that are fundamentally about selecting and filtering records (which is what `published(1)` implies), starting with the Query Builder provides better clarity and often better performance optimization opportunities, especially as your application scales. Consider how this applies to complex relationships or scope definitions. When working with Eloquent, understanding the difference between building a query (`where`, `with`, etc.) and manipulating an existing object helps you write code that is highly readable and adheres to Laravel's design philosophy. This principle is central to effective development practices, much like the principles outlined on the [Laravel Company website](https://laravelcompany.com). ## Best Practice and Conclusion For any operation involving filtering, selecting, or retrieving data from the database based on model criteria, **Approach 1 (using `newQuery()`) is strongly recommended.** It enforces the pattern of building a query before executing it, making your code more explicit about its intent. If you were simply fetching a single record by its primary key, direct instantiation (`User::find(1)`) would be perfectly fine and often more concise. However, when chaining operations like filtering based on attributes (e.g., `$model->published(1)`), the Query Builder approach is superior because it manages the state of the database query explicitly. In summary, always default to starting with a Query Builder (`Model::query()`) when your goal is to build dynamic database queries. This practice leads to more robust, readable, and maintainable code, ensuring you are leveraging Eloquent's full potential for data manipulation.