Pagination Laravel

Asked

Viewed 93 times

0

I want to filter a page by category and create her pagination..

Here is the route

Route::get('/category/{id}',[
'uses' => 'FrontEndController@category',
'as' => 'category.single'
]);

The method

public function category($id)
{
    $category = Category::find($id);

    return view('category')->with('category', $category)
                           ->with('title', $category->name)
                           ->with('settings',Setting::first())
                           ->with('tags', Tag::all());

}

in which I was trying to pick up the categories

   ->with('categories', Post::where('category_id','like',$id)->paginate(1))

But I’m not getting it, it shows the right number of pages but it shows all of them on the page. The HTML part is this

            <div class="col-md-12 col-lg-8">

                <div class="stunning-header stunning-header-bg-lightviolet">
                    <div class="stunning-header-content">
                    <div class="p-titleCategory"><h1>{{$category->name}}</h1></div>     
                    </div>
                </div>

                @if($category->posts->count()>0)
              @foreach($category->posts as $post) 
                                <div class="newsfield">

                <div class="mb-30 p-30 ptb-sm-25 plr-sm-15 card-view">
                <a href="{{ route('post.single', ['slug' => $post->slug]) }}">  
                <div class="time">{{ $post->created_at->diffForHumans() }}</div>                                                
                    <img src="{{ $post->featured }}" alt="{{ $post->title }}">
                    <h3 class="mt-10 ml-20 mr-20 text-center">{{ $post->title }}</h3>

                    <?php 
                            if(strlen($post->content) > 213 )                               
                            $content = substr($post->content,0,strpos($post->content,' ',213));
                            else 
                            $content = $post->content;                                  
                        ?>      
                    <div class="mt-20 ml-20  mr-20">    {!! $content !!}</p></div>
                    <br>
                    <div class="text-center"><b>Clique para continuar lendo.</b></div>
                    </a>
                    <ul class="mtb-10 list-li-mr-20 color-lite-black text-right">                               
                        <li><i class=""></i>Por: {{$post->user->name }}</li>
                        <li>Categoria: <a href="{{ route('category.single',['id' => $post->category->id]) }}"> {{$post->category->name}}</a>    </li>                                   
                    </ul>

                    <div class="widget w-tags">                 
                        <div class="tags-wrap">                             
                            @foreach($post->tags as $tag)
                            <a href="{{ route('tag.single',['id' => $tag->id]) }}" class="w-tags-item">{{$tag->tag}}</a>
                            @endforeach     
                        </div>
                    </div>
                </div><!-- sided-250x -->
            </div>
                                    @endforeach  
                                    @else
                        <h1 class="text-center">Nenhum resultado encontrado.</h1>
                    @endif

            </div>
        </div>
        {{$categories->links()}}  
    </div>      
</div>  

If anyone has any idea how I can fix this :S

  • Good morning man, it’s like giving a dd() in $Categories ?

1 answer

0

I managed to solve, it was my foreach on the php page that was wrong, I changed to

    @if($categories->count()>0)
              @foreach($categories as $post) 

and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.