【发布时间】:2017-10-15 11:31:23
【问题描述】:
我需要一些有关此刀片模板的帮助,它适用于非营利组织的主页,我已在主页上提取了他们的所有类别,我可以深入了解关系以获得所需的新闻,但我有一个问题在我的循环或其他东西中,让我解释一下,我有一个主块,每列有两列8 条新闻格式正确,我的意思是输出中没有重复的条目,谢谢。
家庭控制器
$categories = Category::with('latestNews')
->orderBy('name', 'asc')
->take(9)
->get();
刀片模板
<!-- 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>
【问题讨论】:
-
我没有阅读代码,但您确定在您的数据库中没有两个相似的新闻吗?
-
嗨@Charlie,是的,我很高兴它两次输出相同的记录,但我认为这更多的是我在循环中构建模板的方式,我也遗漏了一些东西,我阅读有关 chunk() 方法的信息,我认为这是我必须根据要求使用的方法。或者可能是这样的条件,如果它是第四项,然后放入 bootsrap 列,然后用其余数据完成循环。
标签: php laravel eloquent blade has-many