显示 laravel 中的帖子

displaying posts in 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, 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>                
                        </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, 2, 3, 4], [5, 6, 7]]