如何解决在laravel中显示帖子
我有一个轮播滑块来显示帖子。我想显示最新的3个帖子,然后在下一张幻灯片上显示其他3个帖子。因此,例如9,8,7-> 6,5,4-> 3,2,1但是我不知道如何使最新的3个处于活动状态,然后在行中显示其他3个帖子。
控制器:
$posts = Post::all();
轮播:
<section class="news">
<h1 class="section-heading text-highlight"><span class="line">Novinky</span></h1>
<div class="carousel-controls">
<a class="prev" href="#news-carousel" data-slide="prev"><i class="fas fa-caret-left"></i></a>
<a class="next" href="#news-carousel" data-slide="next"><i class="fas fa-caret-right"></i></a>
</div><!--//carousel-controls-->
<div class="section-content clearfix">
<div id="news-carousel" class="news-carousel carousel slide">
<div class="carousel-inner">
<div class="item carousel-item active">
<div class="row">
@foreach($posts->take(3) as $post) // Taking 3 posts but not latest
<div class="col-lg-4 col-12 news-item">
<h2 class="title"><a href="news-single.html">{{ $post->title }}</a></h2>
<img class="thumb" src="{{ Storage::disk('public')->url('post/'.$post->image) }}" alt="{{ $post->title }}" />
<p>Suspendisse purus felis,porttitor quis sollicitudin sit amet,elementum et tortor. Praesent lacinia magna in malesuada vestibulum. Pellentesque urna libero.</p>
<a class="read-more" href="news-single.html">Číst více<i class="fas fa-chevron-right"></i></a>
asd
</div><!--//news-item-->
@endforeach
</div><!--//row-->
</div><!--//item-->
<div class="item carousel-item">
<div class="row">
@foreach($posts->take(3) as $post) // Taking the same 3 posts :/
<div class="col-lg-4 col-12 news-item">
<h2 class="title"><a href="news-single.html">{{ $post->title }}</a></h2>
<img class="thumb" src="{{ Storage::disk('public')->url('post/'.$post->image) }}" alt="{{ $post->title }}" />
<p>Suspendisse purus felis,elementum et tortor. Praesent lacinia magna in malesuada vestibulum. Pellentesque urna libero.</p>
<a class="read-more" href="news-single.html">Číst více<i class="fas fa-chevron-right"></i></a>
</div><!--//news-item-->
@endforeach
</div><!--//row-->
</div><!--//item-->
</div><!--//carousel-inner-->
</div><!--//news-carousel-->
</div><!--//section-content-->
</section><!--//news-->
图片:
活动幻灯片:
下一张幻灯片
解决方法
$posts = Post::orderBy('created_at','desc')->get();
,
首先按如下所示创建它们:
$posts = Post::orderByDesc('created_at')->get();
然后在laravel集合中使用chunk
方法来做到这一点:
@foreach ($posts->chunk(3) as $key => $chunk)
<div class="item carousel-item {{ $key == 0 ? 'active' : ''}}">
<div class="row">
@foreach($chunk as $post)
<div class="col-lg-4 col-12 news-item">
<h2 class="title"><a href="news-single.html">{{ $post->title }}</a></h2>
<img class="thumb" src="{{ Storage::disk('public')->url('post/'.$post->image) }}" alt="{{ $post->title }}" />
<p>Suspendisse purus felis,porttitor quis sollicitudin sit amet,elementum et tortor. Praesent lacinia magna in malesuada vestibulum. Pellentesque urna libero.</p>
<a class="read-more" href="news-single.html">Číst více<i class="fas fa-chevron-right"></i></a>
asd
</div><!--//news-item-->
@endforeach
</div><!--//row-->
</div><!--//item-->
@endforeach
此方法将集合分离为所需的部分,例如:
$collection = collect([1,2,3,4,5,6,7]);
$chunks = $collection->chunk(4);
$chunks->toArray();
// [[1,4],[5,7]]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。