0

I have the following query to return the $steps :

$steps = Step::where('g_id', $id)->get();

return view('showg', ['step' => $steps]);

The results are a group of objects looped in a foreach. I'm trying to add a rank to each step, which will show the number of the step. It should simply start from one until the end of the steps.

Is there an easy way instead of creating a step_number column for this purpose ?

 @foreach ($step as $steps)   
     <div>{{ $steps->step_number }}</div>                                             
     <div>{{ $steps->step }}</div>   

 @endforeach            
4
  • By ranking you mean steps with the same values must take the same rank, or you just need a serial number? Commented May 2, 2020 at 15:22
  • Yes, I need a serial number for each step to be shown in the view Commented May 2, 2020 at 15:23
  • Use a simple counter like $counter = 0; before the loop and then inside the loop <div>{{++$counter}}</div> Commented May 2, 2020 at 15:27
  • Use the $loop->iteration laravel.com/docs/7.x/blade#the-loop-variable Commented May 2, 2020 at 15:28

1 Answer 1

1

Try this:

<?php $counter = 0 ?>
@foreach ($step as $steps)   
     <div>{{ ++$counter }}</div>
     <div>{{ $steps->step_number }}</div>                                             
     <div>{{ $steps->step }}</div>   
@endforeach           

I hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.