【问题标题】:Displaying Eloquent data in two bootsrap colums (same row) Laravel在两个引导列(同一行)中显示 Eloquent 数据 Laravel
【发布时间】:2017-10-15 11:31:23
【问题描述】:

我需要一些有关此刀片模板的帮助,它适用于非营利组织的主页,我已在主页上提取了他们的所有类别,我可以深入了解关系以获得所需的新闻,但我有一个问题在我的循环或其他东西中,让我解释一下,我有一个主块,每列有两列8 条新闻格式正确,我的意思是输出中没有重复的条目,谢谢。

visual example

家庭控制器

$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


【解决方案1】:

首先你在 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>

【讨论】:

  • 感谢@Rafael 的标记修复,它就像一个魅力!我尝试了 take(2)->chunk(1) 并得到了预期的结果非常感谢你这将在未来非常有用!
猜你喜欢
  • 2021-12-08
  • 2021-09-08
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
相关资源
最近更新 更多