Property [title] does not exist on this collection instance

PhpLaravelEloquentProperties

Php Problem Overview


I am following Laracasts' videos: https://laracasts.com/series/laravel-5-fundamentals/episodes/9">Basic Model/Controller/View Workflow.

I have a table holds contact information.

CREATE TABLE `about` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

I am trying to pass data to view using the following code in the controller file:

public function index()
{
    $about = Page::where('page', 'about-me')->get(); //id = 3

    return view('about', compact('about'));
}

When I try to show the code as shown below,

@section('title')
    {{$about->title}}
@stop

@section('content')
    {!! $about->content !!}
@stop

I get error that says:

>Property [title] does not exist on this collection instance. (View: E:\laragon\www\newsite\resources\views\about.blade.php)

But if I change the retrieving method in the controller file, it works.

public function index()
{
    $about = Page::find(3);

    return view('about', compact('about'));
}

When I use dd($about) in the first case (where()->get()) the data is encapsulated by an array. In the second case (find(3)) it displays data as expected.

What am i doing wrong?

Php Solutions


Solution 1 - Php

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 }}

Solution 2 - Php

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

Solution 3 - Php

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

in stead of get().

It works on my project. Thanks.

Solution 4 - Php

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;

Solution 5 - Php

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.

Solution 6 - Php

$about->first()->id or $stm->first()->title and your problem is sorted out.

Solution 7 - Php

As result of $about = Page::where('page', 'about-me')->get() is a laravel collection, you can get a specific attribute like title using pluck() method which is described in https://laravel.com/docs/master/collections#method-pluck.

$titles = $about->pluck( 'title' )

@foreach($titles as $title)
    {{ $title }}
@endforeach

Solution 8 - Php

The stored information in about valiable, is in collection format, so you just need to loop through it.

@if(count($about))
   @foreach($about as $page)
    {{$page->title}}
   @endforeach
@endif

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzkanocaView Question on Stackoverflow
Solution 1 - PhpAlexey MezeninView Answer on Stackoverflow
Solution 2 - PhpAlexView Answer on Stackoverflow
Solution 3 - PhpPhantih View Answer on Stackoverflow
Solution 4 - Phpagm1984View Answer on Stackoverflow
Solution 5 - PhpPnj PatelView Answer on Stackoverflow
Solution 6 - PhpOnyash EdView Answer on Stackoverflow
Solution 7 - PhpBABAK ASHRAFIView Answer on Stackoverflow
Solution 8 - PhpbastianyView Answer on Stackoverflow