Laravel 5在3个帖子后显示添加

时间:2016-01-12 14:39:09

标签: php laravel-5 blade laravel-blade

大家好,我已经用变量$lastItems

宣布了我的帖子
@if($lastItems->total() > 0)                   
  @foreach($lastItems as $item)
     @include('._posting.items')                                                  
  @endforeach                                    
@else
     @include('errors.emptycontent')
@endif

我希望在每个帖子后添加广告,我会使用类似这样的内容

@if($lastItems->total() > 0)                   
  @foreach($lastItems as $item)
     @include('._posting.items')                                                  
  @endforeach  
 @if($item = 3)
    <img src="link" alt="ads">
 @endif                                  
@else
     @include('errors.emptycontent')
@endif

但问题是,当我尝试这样做时,它会显示每个帖子之后的广告,而不是在3个帖子之后。 我做错了什么?

2 个答案:

答案 0 :(得分:3)

您可以尝试以下方式:

@if($lastItems->total() > 0)                   
  @foreach($lastItems as $key => $item)

     @include('._posting.items')

     @if (($key + 1) % 3 == 0)
         <img src="link" alt="ads">
     @endif                                                  
  @endforeach                                 
@else
     @include('errors.emptycontent')
@endif

答案 1 :(得分:1)

我有几乎同样的问题,在我的情况下我不得不改变div的课程。所以我用过这样的东西,它帮助了我:

    <?php $var = -1; ?>
@foreach($lastItems as $item)
  <?php $var ++;?>
    @if($var%3 == 0 && $var!=0)
       @include('errors.emptycontent')
    else
        @include('._posting.items')
   @endif
@endforeach

我希望它有所帮助:)