147

I am following Laracasts' videos: 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?

zkanoca
  • 9,664
  • 9
  • 50
  • 94

8 Answers8

367

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 }}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 2
    yes, after execute Model->get() obtain a list with one object, by that reason always show data and I can't access that. After exexute first() then get object – Rubén Ruíz Oct 26 '18 at 17:42
  • 20
    After 4 HOURS of searching, reading and testing Laravel Documentation and visiting a LOT of StackOverflow questions/answers, you were the only one that mentioned that get() retrieves a collection. It was you who made me jump off the chair and realize that all my queries were correct. The problem was on the "print". From the bottom of my heart, a big thank you for this answer! I really mean it! – Grinnex. Aug 20 '20 at 18:18
44

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();
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Alex
  • 2,707
  • 4
  • 29
  • 42
5

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;
agm1984
  • 15,500
  • 6
  • 89
  • 113
4

You can try using first() instead of method get()

$about = DB::where('page', 'about-me')->first();
11Strong
  • 129
  • 6
  • 3
    While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please [edit] your answer - [From Review](https://stackoverflow.com/review/low-quality-posts/21951954) – Nick Jan 17 '19 at 04:00
1

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.

Pnj Patel
  • 1
  • 2
0

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

Onyash Ed
  • 1
  • 2
0

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
SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
0

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
bastiany
  • 21
  • 1
  • 8