how to paginate posts from category

Posted 4 months ago by tastemedia

Hi, im not sure how to make this in a correct way

i get all posts related to my category but don´t know how paginate the results: here my category controller:

class CategoryController extends Controller
    public function show(Category $category) {
        return view('posts.category', compact('category'));



my category model:

class Category extends Model {

    protected $table = 'categories';

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

    public function getRouteKeyName() {

      return 'name';


the blade template:

@foreach($category->posts as $post)

        <div class="col-md-4">
                <div class="card mb-4 shadow-sm">
                <a href="/post/{{ $post->slug }}">  
                    <img src="{{ Voyager::image( $post->image ) }}" style="width:100%">
                    <div class="card-body">
                        <h1 class="card-text">{{ $post->title }}</h1>
                        <p><span>{{ $post->created_at->format('d'."." . 'm' . "." . 'Y') }}</span></p>
                        <p><span class="badge badge-info">{{ $post->category->name }}</span>
                        <span class="badge badge-warning ">free content</span></p>

Any help appreciated, thanks in advance.

Please sign in or create an account to participate in this conversation.