Laravel 5 如何在主页上限制我的laravel帖子

Laravel 5 如何在主页上限制我的laravel帖子,laravel-5,laravel-blade,Laravel 5,Laravel Blade,所以我想知道我是否可以限制我的主页的帖子数量 主页 在这里,我检查是否有任何帖子,如果有,我会全幅显示最新的帖子 @if(count($posts) > 0) {{-- Get first post --}} <div class="col-sm-12" style="padding: 0px;"> <a href="posts/{{$posts->first()->id}}">

所以我想知道我是否可以限制我的主页的帖子数量

主页
在这里,我检查是否有任何帖子,如果有,我会全幅显示最新的帖子

    @if(count($posts) > 0)
        {{-- Get first post --}}
        <div class="col-sm-12" style="padding: 0px;">
            <a href="posts/{{$posts->first()->id}}">
                <div  class="post-icon">
                    <img style="width: 100%;" src="storage/cover_images/{{$posts->first()->cover_image }}">
                </div>
                <div class="bottom-left">
                    <button class="btn  bg-dark text-light" type="submit">
                        <h5>
                            {{$posts->first()->title}};
                        </h5>
                    </button>
                </div>
                <div class="top-right-categorie">
                    <div class="categorie" style="background-color: {{$posts->first()->categorie['hex']}};">
                        {{$posts->first()->categorie['title']}}
                    </div>
                </div>
            </a>
        </div>
        <div class="w-100">
            <br>
        </div>
    @endif
页面

您可以在以下目录中轻松找到:

您可以尝试:

$posts = Post::->take(2)->get();
return view('posts.index')->with('posts', $posts);
这将返回前2篇文章,将“2”替换为您需要的文章数量,您可以使用:

$post = Post::all()->limit(5);
return view('posts.index')->with('posts', $posts);
或使用分页:

$post = Post::all()->paginate(5);
return view('posts.index')->with('posts', $posts);

例如,你只需要3篇文章?
$post = Post::all()->paginate(5);
return view('posts.index')->with('posts', $posts);