highcode
highcode

Reputation: 31

Laravel 5 after 3 posts show an add

Hello guys i have declared my posts with variable $lastItems

@if($lastItems->total() > 0)                   
  @foreach($lastItems as $item)
     @include('._posting.items')                                                  
  @endforeach                                    
@else
     @include('errors.emptycontent')
@endif

and i want to add ads after each 3 post, i use something like this

@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

but the problem is, when i try like this it shows the ads after every post not after 3 posts. What i'm doing wrong?

Upvotes: 1

Views: 632

Answers (2)

Mihai Matei
Mihai Matei

Reputation: 24276

You may try something like:

@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

Upvotes: 3

Alvin
Alvin

Reputation: 894

I had almost same problem, in my case I had to change div's class. So I used sth like this and it helped me:

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

I hope it helps :)

Upvotes: 1

Related Questions