Subquery Attribute Loading with withAttribute() in Eloquent

Punyapal Shah
Load specific relationship column values directly into model attributes using withAttribute() without loading complete child model instances.

When you only need a single field from a related model (such as a author name or category title), eager loading the full model creates unnecessary object allocation overhead.

Using `withAttribute()` attaches subquery selected values directly onto the primary model:

use App\Models\Post;

// Eager load only the category name directly as an attribute
$post = Post::withAttribute('category', 'name')->first();
echo $post->category_attribute_name;

// Lazy load attribute on an existing model instance
$post = Post::first();
$post->loadAttribute('category', 'name');

// Advanced subquery callback formatting
Post::query()
    ->select('id')
    ->withAttribute(['comments as last_comment' => fn ($q) => $q->latest()], 'content')
    ->get();
  • Avoids instantiating nested relationship model objects for simple display values
  • Converts nested queries into efficient sub-select SQL statements
  • Keeps API payload responses lightweight and flat
Tags: #Laravel #Eloquent #Performance
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips