Maaz Zindani

Loading the index…

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

Laravel: reading a column off a collection instead of a model

ErrorException: Property [title] does not exist on this collection instance.

Short answer

You asked a collection for an attribute that belongs to one model. get(), all() and a hasMany relationship return a collection, even when only one row matches. Read the column from one model: use first() or find() in the query, loop over the collection, or call pluck() to get the column from every item.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the class of the value you read the property from. If it is a collection, one of the causes below applies.

    dd(get_class($posts));
  2. 2Match what you see, then jump to its fix

    1. 1Illuminate\Database\Eloquent\Collection from the first step, and a query ending in get() or all()get() or all() was used where one model was neededFix
    2. 2The exception names a column of the related model, for example $post->comments->bodyA hasMany relationship was read like a single modelFix
    3. 3The error is raised in a Blade file, and the controller passes the result of get() to itA Blade view reads the property outside a loopFix

What does the error look like?

  • Exception: Property [title] does not exist on this collection instance.
  • The same code works with find(1) and breaks with where(...)->get()

Cause 1 of 3

get() or all() was used where one model was needed

1Check

These methods return a collection, not a plain array and not a model, even if one row matched.

grep -rn "->get()" app/Http/Controllers/

2Fix

Ask for one model with first() or find(), or firstOrFail() when a missing row should be a 404.

// before
$post = Post::where('slug', $slug)->get();
echo $post->title;

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

3Verify

Reload the page: the title prints with no exception.

// in php artisan tinker
get_class(App\Models\Post::where('id', 1)->firstOrFail());

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

A hasMany relationship was read like a single model

1Check

Reading a one-to-many relationship as a property gives a collection of models, even for one related row or none.

grep -n "hasMany" app/Models/*.php

2Fix

Loop over the collection, or pick one related model before reading its column. To filter, call the relationship as a method and chain first().

foreach ($post->comments as $comment) {
    echo $comment->body;
}

$comment = $post->comments()->where('approved', true)->first();

3Verify

Render the page again and check that each related row prints.

// in php artisan tinker
get_class(App\Models\Post::first()->comments);

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

A Blade view reads the property outside a loop

1Check

The variable handed to the view is a collection, while the template uses it as one record.

grep -rn "->title" resources/views/

2Fix

Loop with @foreach in the view, or use pluck() in the controller to take just the column from every row.

@foreach ($posts as $post)
    {{ $post->title }}
@endforeach

// or, in the controller
$titles = Post::pluck('title');

3Verify

Open the page and confirm every row appears.

// in php artisan tinker
App\Models\Post::pluck('title');

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Name variables by what they hold: $post for one model, $posts for a collection.
  • Use firstOrFail() or findOrFail() in controllers so a missing row is a 404, not a null.
  • Type-hint return values (Post, Collection) so the editor flags the mismatch before you run the page.

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 built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

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