How addSelect() Builder method works in Laravel

How addSelect() Builder method works in Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How addSelect() Breaks Eager Loading in Laravel

When you chain User::with(['flights'])->addSelect(DB::raw('1 as number'))->get(), you are technically asking two different database queries to run against a single model instance, but the result is that your eager-loaded relationships disappear. The core issue isn't that addSelect() is broken; it's that adding columns globally to the main query changes how Eloquent resolves its relationship cache.

Eager loading in Laravel relies on a specific mechanism: it executes a separate SQL query to fetch all related records, then matches them to parent models using a unique identifier (usually the primary key). When you use addSelect() on the root model without specifying which table or relationship it applies to, Eloquent assumes you are adding that column to every table involved in the hydration process.

In your example, DB::raw('1 as number') is added to the User query, but Eloquent's internal logic can interpret this as a directive to modify the join conditions or the select list for the related tables as well. Because the Flight model does not have a number column defined in its schema, and because the raw SQL string doesn't explicitly target the flights table, the query engine fails to hydrate the relationship data correctly. The result is an empty array for flights, even though the parent model data loads fine.

This behavior highlights a fundamental limitation of global modifiers in Eloquent queries. Methods like addSelect() are designed to alter the output of the specific query object they are called on, but they do not respect the isolation required by eager loading unless you explicitly scope them to a table. If you want to add a calculated column, such as your 1 as number example, you must ensure that this modification does not interfere with the separate query Eloquent generates for the relationships.

To achieve your goal of adding a column while maintaining eager loading, you have two primary approaches depending on whether you need that column in the final JSON response or just within your PHP logic.

Using addSelect() on a Specific Relationship

If you need the custom column to appear alongside the relationship data, you must apply the modifier directly to the relationship query builder. Instead of calling addSelect() on the root User query, you can scope it inside the relationship definition or pass it as an argument to the with method.

Here is how you can define a custom column for the eager loading within the relationship:

public function flights()
{
    return $this->hasMany('App\Flight')->addSelect(
        DB::raw('1 as number')
    );
}

By attaching addSelect() to the Flight model's query inside the hasMany definition, you ensure that the raw column is added only to the flights table's SELECT list. The root User query remains untouched regarding column selection, allowing Eloquent to successfully match the flightIDs back to the user records without confusion.

Using $appends for Global Contexts

If your use case involves adding a calculated field that applies to the entire model (like a status flag or a count) and you want it in the JSON response, Laravel provides the $appends property. This allows you to append data to the model's attributes without modifying the SQL SELECT clause at all.

class User extends Model
{
    protected $table = 'User';
    
    protected $fillable = ['username', 'favoriteColor'];

    public function flights()
    {
        return $this->hasMany('App\Flight');
    }
}

// Usage
$user = User::with(['flights'])->find(1);

// This does not affect the SQL query structure at all.
$user->append(['number' => 1]); 

While $appends is excellent for adding metadata to your API response, it operates in the PHP layer after hydration. It cannot be used to add a raw column directly to the database result set. If you specifically need the raw SQL column number in your JSON output while keeping eager loading intact, binding the addSelect() call to the relationship query is the correct architectural decision.

Attempting to force global column additions onto a model that participates in eager loading without scoping those changes will always lead to empty relationships. The trade-off here is clarity: by isolating the raw SQL modification to the specific table it belongs to, you maintain the integrity of Eloquent's hydration cycle.

Our network