Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Laravel throws because a column was read from a query that never ran

Your error

Exception: Property [title] does not exist on the Eloquent builder instance.1 more line it can show

Fix it fast

Most common cause, 1 of 3

The query chain stops before a method that fetches a row

Finish the chain with a method that runs the query and returns one model, or with value() when only one column is needed.

Fix
$post  = Post::where('slug', $slug)->firstOrFail();
$title = $post->title;

// just the column, null when no row matches
$title = Post::where('slug', $slug)->value('title');
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Exception "Property [title] does not exist on the Eloquent builder instance" means a column was read from an Eloquent query that was never executed. where(), with(), orderBy() and local scopes return a Builder, not a model. End the chain with first(), firstOrFail() or find(), or call value('title') for a single column. Applies to Laravel 12 and 13.

  • Applies to Laravel 13.43 (PHP 8.3 to 8.5), Laravel 12 (PHP 8.2 to 8.5)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Dump the class of the variable whose property fails, right where it is read. Illuminate\Database\Eloquent\Builder means the query was built but never run; a model class name means the problem is elsewhere.

Run this first
dd(get_class($post));

Then find the line you see in this list and open its fix:

  1. 1The query chain stops before a method that fetches a rowThe failing line reads a property straight off Post::where(...), Post::with(...), Post::latest() or a local scope such as Post::published()
  2. 2The controller hands an unfinished query to the Blade viewThe stack trace ends in a compiled view under storage/framework/views, and the controller assigns $post = Post::where(...) without a fetch method
  3. 3An API resource wraps a query instead of a modelThe trace runs through a class in app/Http/Resources, for example PostResource::toArray(), and the route does new PostResource(Post::where(...))

What does the error look like?

  • Exception: Property [title] does not exist on the Eloquent builder instance.
  • Property [name] does not exist on the Eloquent builder instance.

Cause 1 of 3

The query chain stops before a method that fetches a row

You seeThe failing line reads a property straight off Post::where(...), Post::with(...), Post::latest() or a local scope such as Post::published()

Check

Laravel throws Property [title] does not exist on the Eloquent builder instance when a column is read from an object that is still a query. Each where(), with(), orderBy() or scope call hands back the Eloquent Builder, and the Builder answers any unknown property with this exception; no SQL has run yet, so there is no row to read from.

Check
$title = Post::where('slug', $slug)->title;   // still a Builder

Fix

Finish the chain with a method that runs the query and returns one model, or with value() when only one column is needed.

Fix
$post  = Post::where('slug', $slug)->firstOrFail();
$title = $post->title;

// just the column, null when no row matches
$title = Post::where('slug', $slug)->value('title');

Verify

The variable now holds an App\Models\Post instance, or the column value.

Verify
dd(get_class($post), $post->title);

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

The controller hands an unfinished query to the Blade view

You seeThe stack trace ends in a compiled view under storage/framework/views, and the controller assigns $post = Post::where(...) without a fetch method

Check

When the exception points into a view, the controller passed the Builder itself. Blade then evaluates {{ $post->title }} against the query object, and the same exception surfaces while the template renders, far from the line that caused it.

Check
// controller
$post = Post::where('slug', $slug);
return view('posts.show', compact('post'));

Fix

Run the query in the controller. firstOrFail() also turns a missing row into a 404 response instead of an error page.

Fix
$post = Post::where('slug', $slug)->firstOrFail();
return view('posts.show', compact('post'));

Verify

The page renders the title, and an unknown slug returns 404.

Verify
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/posts/no-such-slug

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

An API resource wraps a query instead of a model

You seeThe trace runs through a class in app/Http/Resources, for example PostResource::toArray(), and the route does new PostResource(Post::where(...))

Check

A JSON resource forwards every $this->title it reads to the object it wraps. Wrap an unexecuted query and that forwarding lands on the Builder, which throws the same Property does not exist message from inside toArray().

Check
return new PostResource(Post::where('slug', $slug));

Fix

Give the resource a model for a single item, or a fetched collection for a list.

Fix
return new PostResource(Post::where('slug', $slug)->firstOrFail());

return PostResource::collection(Post::where('published', true)->get());

Verify

The endpoint returns JSON with the post's fields.

Verify
curl -s https://example.com/api/posts/hello-world

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Declare return types on repository and service methods (: Post, : Collection); PHP then throws a TypeError where a Builder is returned by mistake, instead of an exception deep in a view.
  • Name query objects and results differently ($postQuery, $post), so a variable that still needs ->firstOrFail() is obvious in review.
  • Give every show route and API resource a feature test that expects 200 for an existing record and 404 for a missing one.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.