Property [title] does not exist on this collection instance
A person might get this while working with factory functions, so I can confirm this is valid syntax:
$user = factory(User::class, 1)->create()->first();
You might see the collection instance error if you do something like:
$user = factory(User::class, 1)->create()->id;
so change it to:
$user = factory(User::class, 1)->create()->first()->id;
When you're using get()
you get a collection. In this case you need to iterate over it to get properties:
@foreach ($collection as $object)
{{ $object->title }}
@endforeach
Or you could just get one of objects by it's index:
{{ $collection[0]->title }}
Or get first object from collection:
{{ $collection->first() }}
When you're using find()
or first()
you get an object, so you can get properties with simple:
{{ $object->title }}
With get()
method you get a collection (all data that match the query), try to use first()
instead, it return only one element, like this:
$about = Page::where('page', 'about-me')->first();
$about = DB::where('page', 'about-me')->first();
in stead of get()
.
It works on my project. Thanks.