I have following code
In controller
public function ViewPost($id)
{
$viewpost= Post::find($id);
return view('Guest.viewpost', ['viewpost'=>$viewpost]);
}
In view
@foreach($viewpost as $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 an error
Trying to get property of non-object.
so i tried following way $val['post_title'];
The above code wont throw an error nor displaying output.
If i print in controller it display output but same in view if i print it give error
print_r($viewpost);
I am using
Thank you.
laravel 5.1.
Can any one tell us what i am doing wrong ?Thank you.
Update
after the suggestion of @CodeRomeos.i can able to display data image not loading.
<img src="{{$val->featured_image}}" height="150" width="150">
<img src="uploads/penguin.jpg">
same works in show view
Answer:
Update your controller like 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 back if you still face the issue.!
No comments:
Post a Comment