Laravel: reading a column off a collection instead of a model
Exception: 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.
Fix it fast
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));2Match what you see, then jump to its fix
- 1
Illuminate\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
The exception names a column of the related model, for example $post->comments->bodyA hasMany relationship was read like a single modelFix - 3
The 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
- 1
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
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/*.php2Fix
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
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
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 says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack 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 fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix