PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 1, 2022

[FIXED] laravel: show both a particular post based on id and featured posts in one page

 February 01, 2022     database, laravel, laravel-5, php, post     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing