Laravel, how to ignore an accessor
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel: How to Ignore an Accessor in Eloquent Queries
As developers working with Eloquent and custom model logic in Laravel, we often encounter scenarios where we define custom accessors—methods that format or compute data for easy retrieval—but we need to query the underlying database fields without including those computed results in our results set. This is a common pain point when dealing with complex models and relationships.
This post will walk you through the specific problem presented, explain why it happens in Eloquent, and provide robust solutions for ignoring accessors during model queries.
## The Scenario: Accessors vs. Database Columns
Let's first look at the setup you described. You have an `Order` model with a custom accessor that calculates data from related models:
```php
class Order extends BaseModel
{
$appends = ['orderContents']; // This tells Eloquent to include 'order_contents' in `toArray()` if accessed via $order->orderContents
public function getOrderContentsAttribute()
{
// This accessor computes related data
return $this->contents()->get();
}
}
```
When you try to query the model:
```php
$openOrders = Order::open()->has('contents')->get(['id', 'date', 'tableName']);
```
The issue arises because Eloquent, when hydrating the results from the database row, often considers attributes that have been defined or computed as part of the object's data structure. Even if you explicitly list columns in `get()`, if the hydration process is influenced by model methods, unintended fields can sometimes slip through, especially if those fields are related to relationships or accessors.
## Why Accessors Interfere with Simple Selections
The behavior you are seeing stems from how Eloquent attempts to hydrate the results. When dealing with dynamic data generated by accessors, Eloquent's default query building might implicitly pull in attributes that define those computed values if they aren't strictly defined as base columns. This can lead to unexpected results when using simple `select()` or `get()` calls on a collection of models.
The core principle here is that you are asking the database for specific *columns*, but Eloquent is dealing with *model hydration*. To ensure you only retrieve raw, physical columns from the table and ignore computed model methods, we need to bypass the standard attribute loading mechanism entirely.
## Solutions: Ignoring Accessors During Querying
There are several effective ways to achieve your goal of ignoring accessors when querying models in Laravel.
### 1. Use Raw Expressions for Strict Column Selection (Recommended)
The most reliable method is to instruct Eloquent to select only the raw database columns you need, bypassing any model-level attribute processing entirely. You can achieve this by using `select()` with raw expressions or by ensuring your selection focuses purely on foreign keys and base attributes.
If you are selecting fields that exist directly on the table (like `id`, `date`), stick to those:
```php
$openOrders = Order::where('status', 'open')
->select(['id', 'date', 'table_name']) // Explicitly select only these columns
->get();
```
If you absolutely need the data computed by the accessor, but don't want it cluttering the result set, you must calculate it *after* retrieval using collection manipulation, rather than relying on Eloquent to fetch it via an accessor.
### 2. Using `with()` Carefully (For Relationships)
When dealing with relationships that trigger accessors, be mindful of the `with()` method. If your accessor relies on a relationship, loading the relationship might inadvertently trigger the accessor logic during hydration. Ensure you only load necessary relationships:
```php
// Only load the required data, avoiding unnecessary relationship loading if possible
$openOrders = Order::where('status', 'open')
->with(['contents' => function ($query) {
// Scope the relationship query if necessary
$query->where('status', 'active');
}])
->get();
```
### Conclusion
Ignoring accessors during Eloquent queries requires a shift in mindset: treat your database query as a request for raw data, not a request for fully hydrated model objects. By leveraging explicit `select()` statements and understanding how Eloquent hydrates models—a concept deeply tied to efficient data retrieval discussed on the [Laravel documentation](https://laravelcompany.com)—you maintain full control over what data is fetched.
For complex scenarios involving computed attributes, it is often better practice to perform the computation *after* fetching the base records in PHP, rather than letting model accessors dictate the structure of your database query. This approach keeps your queries lean, predictable, and highly performant.