4
@foreach ($searchalls as $searchall)
    @php
      $i=1
    @endphp
     {{$i }} ={{$loop->iteration}}%3
     @if($i == 1 )
      <div class="row clearfix">
       @endif 
        <div class="col-md-4 column">
                <h2>
                {{$search_all->id }}Heading
                </h2><p>once OK.
                </p>
                <p>
                     <a class="btn" href="#">View details »</a>
                </p>
       </div>

I got the result on blade is

1 =1%3
1 =2%3
1 =3%3
1 =4%3

cannot got the number remainder?

How can I fix the problem?

Hamza Abdaoui
  • 2,029
  • 4
  • 23
  • 36
robspin
  • 771
  • 3
  • 14
  • 33

2 Answers2

8

Do this

@foreach ($searchalls as $searchall)

@php
    $i = 1;
    $i = $loop->iteration%3;
@endphp

@if( $i == 1 )
    <div class="row clearfix">
@endif

<div class="col-md-4 column">
    <h2>
        {{ $search_all->id }}Heading
    </h2>
    <p>
        once OK.
    </p>
    <p>
        <a class="btn" href="#">View details »</a>
    </p>
</div>

Once your blade template string is rendered you essentially cannot compare with php using == in my opinion which is happening in

{{ $i }} = {{ $loop->iteration }}%3
KalyanLahkar
  • 835
  • 8
  • 21
3

if all your are trying to is to display 3 columns in a row then you can use a better approach using the chunk method

@foreach ($searchalls->chunk(3) as $chunk)
<div class="row clearfix">
    @foreach($chunk as $searchall)
         <div class="col-md-4 column">
            <h2>{{$searchall->id }}Heading</h2>
            <p>once OK.</p>
            <p>
                <a class="btn" href="#">View details »</a>
            </p>
         </div>
    @endforeach
</div>
@endforeach

if you just wants to do math operations inside dom you can use this, or @php @endphp to prevent showing the result

   @foreach ($searchalls as $searchall)
    {{$i = $loop->iteration %3}}
   @endforeach

of immediately inside if statement

   @foreach ($searchalls as $searchall)
       @if($loop->iteration %3 == 1 )
           <div class="row clearfix">
       @endif 
   @endforeach

Edit: use flex box

if you want to make the numbers of dynamic you can use flexbox

   @foreach ($searchalls->chunk(3) as $chunk)
        <div style="display: flex;">
            @foreach($chunk as $searchall)
                <div style="flex:1">{{$searchall->id}}</div>
            @Endforeach
        </div>
    @endforeach

and instead of 3 you set it to any number of columns you want

A. Dabak
  • 760
  • 1
  • 9
  • 23
  • I want to get the data and then use them on the bootstrap on 3 columns. so I use this foreach ($search_alls as $search_all) if($loop->iteration %3 == 1 )
    endif

    {{$search_all->id}}

    if($loop->iteration %3 == 0 )
    endif endforeach But I got the incline 3 column? And how can I fix the question about 4 or 5 or 7 or 8 (not division by the 3) data to satisfy the bootstrap condition?
    – robspin Mar 23 '18 at 09:00
  • I cannot use @foreach on the comment so I use foreach – robspin Mar 23 '18 at 09:02
  • i am starting to get confused, i will make another replay with what i think you mean – A. Dabak Mar 23 '18 at 09:12
  • I acknowledge your help. It's elegant and useful – robspin Mar 23 '18 at 17:17
  • The id I got is the first data id which match the query condition. – robspin Mar 23 '18 at 19:22