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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)