Php laravel如何为单类别帖子分页
如何在单个类别页面中为帖子分页 代码Php laravel如何为单类别帖子分页,php,laravel,Php,Laravel,如何在单个类别页面中为帖子分页 代码 my blade loop <h1>{{$category->name}}</h1> @foreach($category->posts as $post) //post data here @endforeach {{$category->name} @foreach($category->posts as$post) //在这里发布数据 @endforeach 类别与职位的关系为n:n 因为你只需要一个类别,你
my blade loop
<h1>{{$category->name}}</h1>
@foreach($category->posts as $post)
//post data here
@endforeach
{{$category->name}
@foreach($category->posts as$post)
//在这里发布数据
@endforeach
类别与职位的关系为n:n
因为你只需要一个类别,你只需要通过一个类别获得帖子
尝试使用其他方法:
公共功能显示($slug){
$cat=类别::其中('slug',$slug)
->其中('online',true)
->第一个();
$posts=Post::join('category_posts','category_posts.Post_id','=','posts.id'))
->其中('category\u posts.category\u id',$cat->id)
->选择raw('posts.*')
->分页(10);
//返回。。。。
}
在刀片循环中:
{{$cat->name}
@foreach($posts作为$post)
//在这里发布数据
@endforeach
以下是我在几个项目中经常使用的确切示例
控制器:
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.
}
视图:
<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() !!}
身份证件
零件号。
零件名称
模型
零售价
@foreach($bike\u parts作为$bike\u part)
{{$bike_part->part_number}
{{$bike_part->description}
{{$bike\u part->bike\u model}
{{$bike\u part->零售价}
@endforeach
{!!$bike_parts->render()!!}
不要使用first()
,而是使用paginate(count)
下面是一个根据您的代码编写的示例
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)->分页(10) 这里有一种方法可以用来实现多个类别中的分页帖子 但是,我怀疑这将产生一个N+1查询。可能需要使用此工具实现快速加载。有人能证实这一点吗
// Category.php (Model)
public function posts()
{
return $this->hasMany('App\posts', 'post_id', 'id');
}
在刀片文件中
@foreach($categories as $category)
<h1>{{$category->name}}</h1>
@foreach($category->posts()->paginate() as $post)
//post data here
@endforeach
@endforeach
@foreach($categories作为$category)
{{$category->name}
@foreach($category->posts()->paginate()作为$post)
//在这里发布数据
@endforeach
@endforeach
其他人注意:似乎他想对内部“posts”数据进行分页,而不是完整的查询。@Joseph exactlyhi,我的post表没有category\u id
列,它们由第三个表category\u posts
关联,该表将返回错误,因为我的查询是基于slug
查找一个类别,而不是使用paginate()
而不是first()
,我再次假设,我的post表没有category\u id
列,它们由第三个表category\u posts
关联。我很好奇这里2个调用与1个链接调用的性能。@Joseph两个查询需要从数据库中取出它们两次,它按IO消耗时间。但是第一个电话很简单,而且很小。所以我们可以忽略它。如果要将其合并到一个查询中。可以使用where选择
hi,谢谢分享。我并不是说这是一种不好的方式,但我相信有更干净的方式可以做到这一点。
@foreach($categories as $category)
<h1>{{$category->name}}</h1>
@foreach($category->posts()->paginate() as $post)
//post data here
@endforeach
@endforeach