在两个引导列(同一行)中显示 Eloquent 数据 Laravel

Displaying Eloquent data in two bootsrap colums (same row) Laravel

我需要一些关于这个 blade 模板的帮助,它用于 non-profit 组织的主页,我已经在主页上提取了他们的所有类别,我可以深入了解关系以获得所需的新闻,但我的循环或其他问题有问题,让我解释一下,我有一个主块,每列中有两列我想显示与该类别相关的 4 条新闻,但我的循环在每一列上重现相同输出,如何正确格式化 2 列 8 条新闻,我的意思是输出中没有重复的条目,谢谢。

visual example

家庭控制器

$categories = Category::with('latestNews') ->orderBy('name', 'asc') ->take(9) ->get();

Blade 模板

<!-- block_inner -->
<div class="block_inner row">
    <!-- small_list_post -->
    <div class="small_list_post col-lg-6 col-md-6 col-sm-6 col-xs-6">
        <ul>
         @foreach( $category->latestNews->take(8) as $news)         
            <li class="small_post clearfix">
            @if($news->Image_Thumb_Url)
                <div class="img_small_post">
                    <img src="{{$news->Image_Thumb_Url}}" alt="{{$news->title}}">
                </div>
            @endif
                <div class="small_post_content">
                    <div class="title_small_post">
                        <a href="#"><h5>{{ str_limit($news->title, 60, ' ...') }}</h5></a>
                    </div>
                    <div class="post_date"><i class="fa fa-calendar"></i> <em><a href="#">{{$news->created_at->diffForHumans()}}</a></em></div>
                </div>
            </li>
        </ul>
    </div>
    <!-- // small_list_post -->

    <!-- small_list_post -->
    <div class="small_list_post col-lg-6 col-md-6 col-sm-6 col-xs-6">
        <ul>
            <li class="small_post clearfix">
            @if($news->Image_Thumb_Url)
                <div class="img_small_post">
                    <img src="{{$news->Image_Thumb_Url}}" alt="{{$news->title}}">
                </div>
            @endif
                <div class="small_post_content">
                    <div class="title_small_post">
                        <a href="#"><h5>{{ str_limit($news->title, 60, ' ...') }}</h5></a>
                    </div>
                    <div class="post_date"><i class="fa fa-calendar"></i><em><a href="#"> {{$news->created_at->diffForHumans()}}</a></em></div>
                </div>
            </li>
        @endforeach
        </ul>
    </div>
    <!-- // small_list_post -->
</div>

首先你在 foreach 语句中犯了一个错误,它破坏了 html 代码。

试试这个:

<!-- block_inner -->
    <div class="block_inner row">
    @foreach($category->latestNews->take(8)->chunk(4) as $newsChunk)
    <!-- small_list_post -->
    <div class="small_list_post col-lg-6 col-md-6 col-sm-6 col-xs-6">
        <ul>
            @foreach($newsChunk as $news)
            <li class="small_post clearfix">
            @if($news->Image_Thumb_Url)
                <div class="img_small_post">
                    <img src="{{$news->Image_Thumb_Url}}" alt="{{$news->title}}">
                </div>
            @endif
                <div class="small_post_content">
                    <div class="title_small_post">
                        <a href="#"><h5>{{ str_limit($news->title, 60, ' ...') }}</h5></a>
                    </div>
                    <div class="post_date"><i class="fa fa-calendar"></i> <em><a href="#">{{$news->created_at->diffForHumans()}}</a></em></div>
                </div>
            </li>
            @endofreach
        </ul>
    </div>
    <!-- // small_list_post -->
    @endforeach
</div>