Property [title] does not exist on this collection instance

社会主义新天地 提交于 2019-11-25 22:02:06

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();

You Should Used Collection keyword in Controller. Like Here..

public function ApiView(){
    return User::collection(Profile::all());
}

Here, User is Resource Name and Profile is Model Name. Thank You.

Phantih
$about = DB::where('page', 'about-me')->first(); 

in stead of get().

It works on my project. Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!