dimanche 29 juillet 2018

Laravel: retrive and paginate all post belonging to one specific category

Ok, so I saw similar questions asked but none of the answers helped, so forgive me if it seems like this is a duplicate.

I am following a course on Udemy(It's been over a week, the instructor hasn't answered my question, although they are active on the site, hence why I'm here), the codes I'm displaying below is how the instructor has taught us to write it.

THE ISSUE: We are creating a blog and that blog has many categories and many posts but each post belongs to only ONE category which can hold multiple posts. Here's a look at the model for them.

public function user(){
    return $this->belongsTo('App\User');
}

public function posts(){
    return $this->hasMany('App\Post');
}

POST MODEL

public function user(){
    return $this->belongsTo('App\User');
}

public function category(){
    return $this->belongsTo('App\Category');
}

They each have their own controller also, we also have a frontend controller that displays all the categories and when you click read more it takes you to a single page that displays the most recent post within that category. This single page also has a pagination that supposed to display the previous post that belongs to that category. INSTEAD: it takes the user to the most recent post regardless of what category it belongs to and display all the previous post for all category. Below is the frontend controller

        public function index()
{
    return view('categories')
           ->with('categories',Category:orderBy('created_at', 'desc')->take(24)->get())
           ->with('tag', Tag::all())
           ->with('posttitle', Post::orderBy('created_at', 'desc')->first());
}

public function tag($id)
{
    $tag = Tag::find($id);

    return view('tag')->with('tag', $tag)
                        ->with('categories', $tag->tag)
                        ->with('categories', Category::all());
}

public function singlePage($slug)
{
    $post = Post::where('slug', $slug)->first();

    $next_id = Page::where('id', '>', $page->id)->min('id');
    $prev_id = Post::where('id', '<', $post->id)->max('id');

    return view('single')->with('post', $post)
                                 ->with('categories', Category::all())
                                 ->with('posttitle', Post::take(1)->get())
                                 ->with('next', Post::find($next_id))
                                 ->with('prev', Post::find($prev_id));                                     
}

Here's a look at the view for the index function

<div class="container">

<div class="container-fluid">
    <div class="row medium-padding120 bg-border-color">
        <div class="container">
            <div class="col-lg-12">
            <div class="offers">
                <div class="row">
                    <div class="col-lg-8 col-md-12 col-sm-12 col-xs-12">
                        <div class="heading">
                            <h4 class="h2 heading-title">Discover New Articles</h4>
                            <div class="heading-line">
                                <span class="short-line"></span>
                                <span class="long-line"></span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="case-item-wrap">
                        @foreach($categories as $category)
                        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
                            <div class="case-item">
                                <div class="case-item__thumb">
                                    <img src="" alt=""style="width:300px; height:280px;">
                                </div>
                                <h6 class="case-item__title text-center"><a href="#">{!! $category->title !!}</a></h6>
                                <!--<i class="seoicon-clock"></i>
                                 <time class="published" datetime="2016-04-17 12:00:00">
                                    
                                 </time>-->
                                <h6 style="width:300px; height:85px;">{!! str_limit($category->summary, 85) !!} 
                                    <small> <a href="">Read More</a></small>
                                </h6>
                            </div>
                        </div>
                        @endforeach
                    </div>
                </div>

            <div class="padded-50"></div>
        </div>
        </div>
    </div>
</div>

and this is the single page view below

    <div class="content-wrapper"> <div class="container"> <div>
<main class="main">
  <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-1 col-md-7 col-md-offset-1 col-lg-6 col-lg-offset-3 panel panel-reading">
      <article class="hentry post post-standard-details">
        <h2>{!! $post->posttitle !!}</h2>
        <div class="post__content">

          <div class="post-additional-info">
            <div class="post__author author vcard">
              By:
              <div class="post__author-name fn">
                <a href="#" class="post__author-link"></a>
              </div>
            </div>

            
            <span class="post__date">
              <time class="published" datetime="2016-03-20 12:00:00">
                <i class="seoicon-clock"></i> 
              </time>
            </span>
            
          </div>

          
          <div class="post__content-info">
            <p> {!! $post->content !!} </p>
          </div>
          

        </div>

        <div class="pagination-arrow">
        
           @if($prev)
            <a href="" class="btn-prev-wrap">
                <svg class="btn-prev">
                  <use xlink:href="#arrow-left"></use>
                </svg>
                <div class="btn-content">
                  <div class="btn-content-title">Previous Page</div>
                    <p class="btn-content-subtitle"></p>
                </div>
            </a>
          @endif 

      
         @if($next)
           <a href="" class="btn-next-wrap">
             <div class="btn-content">
               <div class="btn-content-title">Next Page</div>
                 <p class="btn-content-subtitle"></p>
              </div>
              <svg class="btn-next">
                <use xlink:href="#arrow-right"></use>
              </svg>
            </a>
              @endif
        </div>

      </article>
  </div>
</main>

HERE'S the post database

    public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('category_id');
        $table->string('posttitle');
        $table->string('slug');
        $table->text('content');
        $table->softDeletes();
        $table->timestamps();
    });
}

IN CONCLUSION: Using this how would you filter it so that only the post that has the same category_id is displayed when the user clicks on that category?

Thank you As a bonus I did a short screen recording of the problem https://www.screencast.com/t/vVzRMSunH



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2AkVO9v
via IFTTT

Aucun commentaire:

Enregistrer un commentaire