Alright so I'm pretty new to Blade, and I did manage to get all the results that I asked for on my page. Now I want to show only 10 of the total items on my page and I seem to struggle with it, tried the array_slice without any success so far. Any suggestions?

Below the code I'm currently using

        {{--@foreach ($element['subs']->slice(0, 10) as $item)--}}

@foreach ($element['subs'] as $item)
        <div class="highlight {{ $element['class'] }}">
            <div class="el-inner-news">

                <div class="image-news">
                    <a href="{{ $item['news-item']['slug'] }}"> <img src="{{ $item['news-item']['image'] or "/assets/frontend/baywest/images/newsholder.png" }}" class="center-img" alt="{{ $item['news-item']['title'] }}" /> </a>
                </div>

                <div class="desc-news">
                    <div class="title-highlight">
                        <a href="{{ $item['news-item']['slug'] }}">{{ $item['news-item']['title'] }}</a>
                    </div>

                    <div class="text-highlight">
                        {!! $item['news-item']['textfield'] !!}
                    </div>

                    <div class="learn-more-news">
                        <a href="{{ $item['news-item']['slug'] }}">{{ $item['news-item']['read-more'] or "Learn more" }}  </a>
                    </div>
                </div>

            </div>
        </div>
    @endforeach

Thanks in advance!

推荐答案

您应该在控制器中限制结果,但以下是如何在Blade 中实现这一点.不漂亮.

<?php $count = 0; ?>
@foreach ($element['subs'] as $item)
    <?php if($count == 10) break; ?>
    // Your code
    <?php $count++; ?>
@endforeach

Laravel相关问答推荐

@vite指令在使用laravel vite构建后导致错误

密码确认在Livewire中不匹配

Laravel:在 PHPUnit 的覆盖率报告中包含文件夹(如果在 app/ 文件夹之外)?

使用命令行界面停止 laravel 服务器

语法错误或访问冲突:1115 未知字符集:utf8mb4

允许的内存大小 134217728 字节用尽(试图分配 20480 字节) Laravel

为什么人们将 .env 放入 gitignore?

file_put_content ...无法打开流:Laravel 5 中的权限被拒绝

在 Laravel 中下载后如何重定向?

Laravel 随机排序

Laravel 和 PHPStorm 项目的 gitignore 中包含什么?

Laravel:自定义或扩展通知 - 数据库模型

Laravel Mix 生成字体到另一个目录

如何在 Laravel 中正确安装软件包?

遍历 Laravel 控制器中的结果集

[Vue 警告]:找不到元素:#app

登录后 DevTools 无法解析 SourceMap 错误将重定向到该 js 文件

如何从 PHPUnit 测试设置运行 Laravel 数据库 seeder 机?

Laravel 如何在 Eloquent 模型中添加自定义函数?

Laravel 5 Auth注销不起作用