php - Trying to get property of non-object in laravel -


i have following code

in controller

  public function viewpost($id)         {              $viewpost= post::find($id);               return view('guest.viewpost', ['viewpost'=>$viewpost]);         } 

in view

  @foreach($viewpost $val)          <br>         post title=         <a href="viewpost/{{$val->id}}" >{{$val->post_title}}</a>       <br>         post content={{$val->post_content}}         <br>  <br>         featured image=<img src="{{$val->featured_image}}" height="150" width="150">       @endforeach 

but above code throw error trying property of non-object. tried following way

 $val['post_title']; 

the above code wont throw error nor displaying output.

if print in controller display output same in view if print give error

 print_r($viewpost); 

i using laravel 5.1. can 1 tell doing wrong ?
thank you.

update

after suggestion of @coderomeos.i can able display data image not loading.

<img src="{{$val->featured_image}}" height="150" width="150">  <img src="uploads/penguin.jpg"> 

same works in show view

update controller this

public function viewpost($id) {      $viewpost= post::find($id)->first();       return view('guest.viewpost', ['viewpost'=>$viewpost]); } 

or

public function viewpost($id) {      $viewpost= post::find($id)->get();       return view('guest.viewpost', ['viewpost'=>$viewpost]); } 

do come if still face issue.!


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -