0

I have a simple eloquent query:

$comments = Comment::where('approved',1)->orderBy('created_at','desc')->get();

return view('comments.approved', compact('comments'));

I am trying to access the data in view using the following syntax

@foreach($comments as $comment)

{{ $comment->content }}

@endforeach

I am getting the following error

Trying to get property of non-object (View: ...

I was able to resolve the problem by either of these

{{ @$comment->content }}
or
{{ $comment['content'] }}

However, I fail to understand why get() is returning an array instead of collection. Isn't get() suppose to return collection of objects?

1 Answer 1

2

PHP compact() does this, according to http://php.net/manual/en/function.compact.php

Creates an array containing variables and their values. 

So you're changing it to an array as you pass it to your view. Try it without using compact() and see if you get the response you're expecting.

return view('comments.approved', ['comments' => $comments]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.