简体   繁体   中英

laravel how to paginate single category posts

How can I paginate posts in single category page?

Code

public function show($slug) {
   $category = Category::where('slug', $slug)->where('online', true)->with(['posts' => function ($q) {
            $q->orderBy('id', 'desc');
            //I need this query be paginate
   }])->first();
   //return....
}

my blade loop

<h1>{{$category->name}}</h1>

@foreach($category->posts as $post)
  //post data here
@endforeach

The relationship of categories and posts is n:n;

Because you just need one category, you just need to get posts by one category.

Try to use another method:

public function show($slug) {

   $cat = Category::where('slug', $slug)
                     ->where('online', true)
                     ->first();

   $posts = Post::join('category_posts', 'category_posts.post_id', '=', 'posts.id')
                ->where('category_posts.category_id', $cat->id)
                ->selectRaw('posts.*')
                ->paginate(10);
   //return....
}

And in your blade loop:

<h1>{{$cat->name}}</h1>

@foreach($posts as $post)
  //post data here
@endforeach

here is the exact example which I always use in my several project.

Controller:

public function index(Request $request){
   $parts = BikeParts::paginate(5); //you can change how many you want


   return view('admin.bike_parts.index_excel', compact(['bike_parts']))->with('i', ($request->input('page', 1) - 1) * 5); // put exact number you are paginating , in here I used 5.
}

View:

  <table class="table table-actions-bar m-b-0">
  <tbody>
 <thead>
       <tr>
        <th>id</th>
         <th>PartNo.</th>
         <th>Part Name</th>
         <th>model</th>
         <th>Retail Price</th>
         </tr>
</thead>
 @foreach($bike_parts as $bike_part)
    <tr>
        <td><a href="#,{{$bike_part->id}}">{{$loop->index+1}}</a></td>
        <td>{{$bike_part->part_number}}</td>
        <td>{{$bike_part->description}}</td>
        <td>{{$bike_part->bike_model}}</td>
        <td>{{$bike_part->retail_price}}</td>
    </tr>
 @endforeach
</tbody>
</table>
{!! $bike_parts->render() !!}

Instead of using first() , use paginate(count)

Here is an example according to your code,

public function show($slug) { 
    $category = Category::where('slug', $slug)
        ->where('online', true)
        ->with(['posts' => function ($q) { 
            $q->orderBy('id', 'desc');  
        }])->paginate(10); 
    //return.... 
}

$posts = Post::where('category_id', $categoryId)->paginate(10);

Heres a way that may be used to achieve paginated posts in multiple category

However, I suspect that this will make a N+1 query. Might need eager loading implemented with this. Can someone confirm this?

// Category.php (Model)
public function posts()
{
    return $this->hasMany('App\posts', 'post_id', 'id');
}

In the blade file

@foreach($categories as $category)
    <h1>{{$category->name}}</h1>

    @foreach($category->posts()->paginate() as $post)
        //post data here
    @endforeach
@endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM