Get Specific Columns Using “With()” Function in Laravel Eloquent

Get Specific Columns Using “With()” Function in Laravel Eloquent

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

Stefan Bogdanescu

Founder & Senior Architect

Selecting Specific Columns in Eloquent with() Relationships

When you need to eager load a relationship using with(), Laravel defaults to selecting all columns (SELECT *) from the related model. This behavior ensures that every available attribute is present in your collection, which works well for most scenarios but can lead to unnecessary data transfer when you only need a few fields. For instance, if you are building a list of posts and only require the id and username from the user table, fetching the entire users table row adds overhead without providing value.

While Laravel's Query Builder allows you to specify columns via the select() method before calling with(), doing so inside an Eloquent model often feels clunky or unintuitive. You might be tempted to chain methods like $this->hasMany('post')->select(...) within your relation definition, but this approach is generally discouraged because it mixes concerns: defining a relationship should describe the association, not dictate the query structure.

To achieve specific column selection in an eager-loaded relationship without switching to raw Query Builder syntax inside your model, you must leverage the with method's second argument. This parameter accepts an array of callbacks that define how each relationship should be queried. By passing a closure for the 'user' relation, you can explicitly control which fields are retrieved.

Here is how you can modify your Post model to fetch only specific columns from the related user:

public static function getAllPosts() {
    return Post::with(['user' => function ($query) {
            $query->select('id', 'username');
        }])
        ->get();
}

In this example, the closure passed to the user key within the with array allows you to run a custom query against the users table. The resulting SQL will look like this:

select * from posts
select id, username from users where users.id in (1, 2)

This approach keeps your model definitions clean while giving you granular control over data retrieval. It also ensures that your eager loading logic remains consistent with the rest of your application's architecture.

One important trade-off to consider is how this affects your resulting objects. When you use select() inside the with closure, Eloquent will only hydrate the attributes specified in your query. If your controller or view expects additional fields that were not selected (such as email or created_at), those properties will be missing from the model instance. This can lead to undefined property warnings if you try to access them directly.

If you need to access those extra fields while still limiting the database load, you might consider using the append feature in Eloquent. You can append virtual attributes that are calculated on the fly or loaded separately without polluting your main query. However, this adds complexity and may not be necessary if your data requirements are static.

Another consideration is performance. While selecting fewer columns reduces network traffic and memory usage on the server, it requires careful planning to ensure you don't break downstream code that assumes certain fields exist. Always verify that any logic dependent on the related model's attributes has been updated accordingly.

For more advanced use cases, such as filtering or transforming data during eager loading, Laravel provides flexible options within these closures. You can also chain additional query methods like where() inside the closure if needed. However, keep in mind that complex queries inside with closures should be kept simple to maintain readability and avoid accidental N+1 issues.

If you find yourself needing reusable patterns for eager loading with custom column selections, consider creating a trait or a dedicated repository class to encapsulate this logic. This keeps your models lean and focused on defining relationships rather than managing query details.

For further reading on optimizing database queries in Laravel, visit https://laravelcompany.com to explore best practices for scaling your application efficiently.

Our network