Laravel: get() returning array? -
i have simple eloquent query:
$comments = comment::where('approved',1)->orderby('created_at','desc')->get(); return view('comments.approved', compact('comments'));
i trying access data in view using following syntax
@foreach($comments $comment) {{ $comment->content }} @endforeach
i getting following error
trying property of non-object (view: ...
i able resolve problem either of these
{{ @$comment->content }} or {{ $comment['content'] }}
however, fail understand why get() returning array instead of collection. isn't get() suppose return collection of objects?
php compact() this, according http://php.net/manual/en/function.compact.php
creates array containing variables , values.
so you're changing array pass view. try without using compact() , see if response you're expecting.
return view('comments.approved', ['comments' => $comments]);
Comments
Post a Comment