如何将我的特色帖子排除在所有帖子循环中
在我看来,我有2部分,分别通过了一个特色帖子和所有帖子。我遇到的问题是试图从所有帖子部分中删除特色帖子,因为它已经在其自己的独特容器中显示。与第一个联系的第二个问题,如果我要创建另一个帖子并将其推出,那么我将如何制作最后一个默认值为正常帖子的帖子IE 0 =正常帖子,1 =特色帖子
索引索引视图
//Featured post
@foreach ($featuredPosts as $featured)
<a href="/posts/{{$featured->id}}">
<img src="{{ asset('assets/frontend/img/beach.jpg') }}" class="featured-post-img">
<figcaption class="article-title-container">
<div class="position-absolute article-tag">
<span class="text-white tag-blue">{{ $featured->tags }}</span>
</div>
<div class="position-relative">
<h1 class="article-title text-dark">{{ $featured->title }}</h1>
</div>
</figcaption>
@endforeach
//All posts
@foreach ($allPosts as $post)
<div class="col-xl-12">
<div class="row g-3 mb-2">
<div class="col-xl-6">
<a href="/posts/{{$post->id}}"><img src="{{ asset('assets/frontend/img/midoriya.jpg') }}"></a>
</div>
<div class="col-xl-6 related-posts-heading">
<a href=""><p class="text-dark">{{ $post->title }}</p></a>
</div>
</div>
</div>
@endforeach
post Post Controller
// Show all listings
public function index(Request $request) {
Paginator::useBootstrap();
$allPosts = Posts::latest()->get();
$paginate = Posts::latest()->paginate(6);
$featuredPosts = $allPosts->filter(function($query) {
return $query->featured == true;
});
return view('frontend.posts.index', compact('allPosts', 'featuredPosts', 'paginate'));
}
Posts::create([
'title' => $request->post('title'),
'sub_title' => $request->post('sub_title'),
'tags' => $request->post('tags'),
'content' => $request->post('content'),
'featured' => ($request->has('featured')) ? true : false,
]);
In my view, I have 2 section where I'm passing a featured post and all posts respectively. The issue I'm having is trying to remove the featured post from the all posts sections since it already being displayed in it's own unique container. Second question that ties in with the first, if I were to create another post and make it featured, how would I make the post that was last featured default to a normal post i.e 0 = normal post, 1 = featured post
Index View
//Featured post
@foreach ($featuredPosts as $featured)
<a href="/posts/{{$featured->id}}">
<img src="{{ asset('assets/frontend/img/beach.jpg') }}" class="featured-post-img">
<figcaption class="article-title-container">
<div class="position-absolute article-tag">
<span class="text-white tag-blue">{{ $featured->tags }}</span>
</div>
<div class="position-relative">
<h1 class="article-title text-dark">{{ $featured->title }}</h1>
</div>
</figcaption>
@endforeach
//All posts
@foreach ($allPosts as $post)
<div class="col-xl-12">
<div class="row g-3 mb-2">
<div class="col-xl-6">
<a href="/posts/{{$post->id}}"><img src="{{ asset('assets/frontend/img/midoriya.jpg') }}"></a>
</div>
<div class="col-xl-6 related-posts-heading">
<a href=""><p class="text-dark">{{ $post->title }}</p></a>
</div>
</div>
</div>
@endforeach
Post Controller
// Show all listings
public function index(Request $request) {
Paginator::useBootstrap();
$allPosts = Posts::latest()->get();
$paginate = Posts::latest()->paginate(6);
$featuredPosts = $allPosts->filter(function($query) {
return $query->featured == true;
});
return view('frontend.posts.index', compact('allPosts', 'featuredPosts', 'paginate'));
}
Posts::create([
'title' => $request->post('title'),
'sub_title' => $request->post('sub_title'),
'tags' => $request->post('tags'),
'content' => $request->post('content'),
'featured' => ($request->has('featured')) ? true : false,
]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论