Issue
I have one post information page:- posts/show.blade.php.
On this page, I have one post information field and featured posts field.
In my controller, I have already put the following code to show one particular post based on ID.
ResultsController.php
public function show($id,Post $post)
{
$post= Post::find($id);
$post->category;
$post ->tags;
return view('posts.show',compact('post'));
}
web.php
Route::get('results/{id}', 'ResultsController@show')->name('posts.show');
show.blade.php
//one of the cards for featured posts
<div class="image">
<img src="../image/image2.jpg" alt="" width="300px" height="200px">
</div>
<div class="card-information">
<div class="event-name">
lorem
</div>
<div class="heart">
<i class="fas fa-heart fa-lg" style="color: #F70661"></i>
</div>
<div class="event-date">
2019.8.23
</div>
<div class="card-info">
<p>Lorem ipsum dolor sit amet in Lorem, ipsum dolor
<a href="#" style="color: white">...see more</a>
</p>
</div>
</div>
But also, I need to show featured events cards bottom of the same page(show.blade.php). I am thinking to add this code to my controller.
$posts = Post::latest()->limit(6)->get();
$categories = Category::latest()->limit(6)->get();
Because I want to show 6 featured posts.
So the question is how to show one particular post and the featured posts on one page.
I am glad if someone helps me out.
Solution
As @kerbholz defined in the comments you can pass multiple data in the view. We can also assign both posts to single array. Your show function should look like this.
public function show($id,Post $post)
{
$particular_post= Post::find($id);
$featured_posts = Post::latest()->limit(6)->get();
// we can assign them to an array
$posts['particular_post'] = $particular_post;
$posts['featured_posts'] = $featured_posts;
//here we can pass this array to the compact function
return view('posts.show',compact('posts'));
}
In the view you can access them like this
//for featured_posts posts you need foreach loop
@foreach($posts['featured_posts'] as $featured_post)
{{ $featured_post->category }}
@endforeach
//for particular post you can just echo variable
{{ $posts['particular_post']->category }}
Answered By - Zain Farooq
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.