Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the +-*/% on laravel blade?

@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?

like image 853
robspin Avatar asked Oct 24 '25 18:10

robspin


2 Answers

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
like image 92
KalyanLahkar Avatar answered Oct 26 '25 06:10

KalyanLahkar


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

like image 33
A. Dabak Avatar answered Oct 26 '25 06:10

A. Dabak