Ruka Xing
Ruka Xing

Reputation: 642

Laravel increment id

I have 5 foreachs. So how do i increment i like



View

@foreach($questions as $question)
 {{ $i }}
@endforeach

@foreach($inquiries as $question)
 {{ $i }}
@endforeach

@foreach($queries as $question)
 {{ $i }}
@endforeach

@foreach($examinations as $question)
 {{ $i }}
@endforeach

@foreach($inquisitions as $question)
 {{ $i }}
@endforeach

Upvotes: 1

Views: 2899

Answers (4)

Tpojka
Tpojka

Reputation: 7111

You can use blade's variable dedicated to this:

@foreach($questions as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($inquiries as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($queries as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($examinations as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($inquisitions as $question)
    {{ $loop->iteration }}
@endforeach

Blade docs.

Upvotes: 2

kshitij
kshitij

Reputation: 710

if want your $i to change after every loop, you should be passing variable by reference.Check the article in the link below: link: http://php.net/manual/en/language.references.pass.php

Upvotes: 1

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try to below way.

<?php 
$i = 0;
?>
@foreach($questions as $question)
 {{ $i++ }}
@endforeach

@foreach($inquiries as $question)
 {{ $i++ }}
@endforeach

@foreach($queries as $question)
 {{ $i++ }}
@endforeach

@foreach($examinations as $question)
 {{ $i++ }}
@endforeach

@foreach($inquisitions as $question)
 {{ $i++ }}
@endforeach

Upvotes: 1

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4040

@php
  $i = 0;
@endphp
@foreach($questions as $question)
 {{ $i }}
  $i++;
@endforeach

use $i in each foreach loop as incremented $i++

Upvotes: 1

Related Questions