duanmao1975 2017-10-15 11:31
浏览 36
已采纳

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

I need some help with this blade template, it's for the homepage of a non-profit organisation, I have pulled all their categories on the homepage and i can drill down in the relationship to get the desired news but I have a issue in my loop or something, let me explain, I have one main block with two columns in each column I would like to display 4 news related to that category, but my loop reproduces on each column the same output, how do I get 2 columns with 8 news correctly formated, by that I mean no duplication of entries on output, thanks ahead.

visual example

HomeController

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

Blade Template

<!-- 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>
  • 写回答

1条回答 默认 最新

  • dongshi4589 2017-10-15 13:39
    关注

    First you made an error in foreach statement which is breaking html code.

    try this:

    <!-- 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了