migdalius's avatar

Pagination and status post/page problem

In page and post i have column status "Active" and "Inactive", i display list of blogs list etc.


@foreach($pages as $page)
@if($page->status == 'ACTIVE')
<article class="card card-product">
  <div class="card-body">
  <div class="row">
    <aside class="col-sm-3">
      <div class="img-wrap"><img src="{{ Voyager::image( $page->img_large ) }}"></div>
    </aside> <!-- col.// -->

    <article class="col-sm-6">
        <h4 class="title">{{ $page->title }}</h4>
        <div class="rating-wrap  mb-2">
 
          <p>
          <span class="meta-wrap author-meta">
              Ocena hurtowni: 
          </span>
          {!! str_repeat('<i class="fa fa-star checked" aria-hidden="true"></i>', $page->ocena_hurtowni) !!}
          {!! str_repeat('<i class="fa fa-star " aria-hidden="true"></i>', 10 - $page->ocena_hurtowni) !!}
          </p>
        </div> <!-- rating-wrap.// -->
        <p> {{$page->excerpt}} </p>
        <dl class="dlist-align">
          <dt>Rodzaj</dt>
          <dd>{{$page->kategoria}}</dd>
        </dl>  <!-- item-property-hor .// -->
        <dl class="dlist-align">
          <dt>Adres:</dt>
          <dd>{!!$page->adres_hurtowni!!}</dd>
        </dl>  <!-- item-property-hor .// -->  <!-- item-property-hor .// -->
        <dl class="dlist-align">
          <dt>Rok założenia:</dt>
          <dd>{{$page->rok_zalozenia}}r.</dd>
        </dl>
      
    </article> <!-- col.// -->
    <aside class="col-sm-3 border-left">
      <div class="action-wrap">
         <!-- info-price-detail // -->
        <p class="text-success">Darmowa dostawa od</p>
        <div class="price-wrap h4">
          <span class="price"> {{$page->darmowa_dostawa}}</span>  
        </div>
        <br>
        <p>
          <a href="/hurtownie/{{ $page->slug }}" class="btn btn-primary"> Recenzja dostawcy</a>
        </p>
        <span>Warunki współpracy</span>
        <a >{!!$page->warunki_wspolpracy!!}</a>
      </div> <!-- action-wrap.// -->
    </aside> <!-- col.// -->
  </div> <!-- row.// -->
  </div> <!-- card-body .// -->
</article> <!-- card product .// -->

@endif
@endforeach

{{ $pages->links() }}

It's work fine, when i turn active they show data, when i inactive they dont show data. But pagination works with all pages, even if is "inactive", and view look ridiculous when i have 3 page and 25 pagination links.

Question: how to make pagination Active page/post ?

0 likes
1 reply
Szach's avatar

I had a similar issue. I only wanted to show active posts on blog page. I fixed it after seeing your post. In controller I am using if statement to pass value of $post. If we don't give a default value the error I got was post variable not defined. Work in progress so I will be refactoring it later.

In controller 
public function index(){

    
    
    $posts=Post::where('status','=',1)->orderBy('published_at', 'DESC')->paginate(8);
    if(!empty($post)){
        return view('frontend.blog',compact('posts'));
    }
    else{
               $post='0';
        return view('frontend.blog',compact('posts'));
    }
    
    
   }
In view 
<div class="card-deck">
          @if($post=!0)
                 @foreach($posts as $post)
                  <div class="card" style="min-width:300px; margin-top:30px; margin-bottom: 30px;">
                    <a href="{{route('blog.page',[$post->slug])}}"><img src="{{asset('storage/'.$post->image)}}" 
                                       class="card-img-top" alt="..."></a>
                    <div class="card-body">
                      <div style="margin-bottom:10px;">
                        <button type="button" class="btn btn-primary btn-sm" disabled>Category</button>
                      </div>
                      <h3 class="card-title">{{$post->post_title}}</h3>
                      <div>
                           <small class="text-muted">By:{{$post->author}}</small>|
                           <small class="text-muted">{{date('d-M-Y', strtotime($post->published_at))}}</small>
                          
                      </div>
                      <div style="margin-top:10px;">
                      {!!html_entity_decode(str_limit($post->content,40) )!!}
                      <a href="{{route('blog.page',[$post->slug])}}">Read more ></a>
                      </div>
                    </div>
                    <div class="card-footer">
                     
                      <small class="text-muted">Comments </small><i class="fas fa-comments fa-sm"></i> |
                      <small class="text-muted">Likes </small><i class="far fa-heart fa-sm"></i>|
                      <small class="text-muted">Views </small><i class="far fa-eye fa-sm"></i>
                    </div>
                  </div>

                  @endforeach
                                @endif
          
            </div>
      @if(!empty($post))
      {{ $posts->links()}}
      @endif

Critique of this code by seniors is welcome since I am a beginner and I may be using coding techniques which cause bugs later. PS: The code formatting seems to have gone wrong and I don't know how to fix it inside the comment form

Please or to participate in this conversation.