@trevorpan the iteration on the loop is not an array of items it is just this
The current loop iteration (starts at 1).
Take a look at the documentation
If you wanted to achieve this in a blade file how would you do it?
{{ $loop->iteration['A'] }}
@trevorpan the iteration on the loop is not an array of items it is just this
The current loop iteration (starts at 1).
Take a look at the documentation
Yea, I was going through that and wanted to increment 1 as 'A', 2 as 'B', etc.
From what I know loops start at e.g. $variable[0]
Have been trying this afternoon to loop variables and then display them as Bidder A, Bidder B, Bidder C, etc. The $loop->iteration was so clean in blade, it'd be neat if there was an increment on string version.
@trevorpan $loop->index starts from 0. You can make a lookup table, like a translation array for example, and for each index.. use the letter..
as an example in your lang/en/messages.php add this
<?php
return [
1 => 'A',
2 => 'B'
// so on for the other letters
];
Then in the loop use this:
<h1>Bidder @lang('messages.' . $loop->iteration)</h1>
{{ chr(64+ $loop->iteration) }}
only good up to 26 bidders
That's pretty awesome. If it hits 26 bidders, I'll be happy to fix that bug!!
But I did go deeper in, and see @nakov could be used here. Both approaches are somewhat limited—manually adding up to a 100 keys e.g. is not what I was hoping for.
Using PHP's A++ would be ideal.
So, I took a look at this file:
<?php
namespace Illuminate\View\Concerns;
use Countable;
use Illuminate\Support\Arr;
trait ManagesLoops
{
/**
* The stack of in-progress loops.
*
* @var array
*/
protected $loopsStack = [];
/**
* Add new loop to the stack.
*
* @param \Countable|array $data
* @return void
*/
public function addLoop($data)
{
$length = is_array($data) || $data instanceof Countable ? count($data) : null;
$parent = Arr::last($this->loopsStack);
$this->loopsStack[] = [
//'iteration' => 0,
//'index' => 0,
'letter' => 'A',
//'remaining' => $length ?? null,
// 'count' => $length,
//'first' => true,
//'last' => isset($length) ? $length == 1 : null,
//'odd' => false,
//'even' => true,
//'depth' => count($this->loopsStack) + 1,
//'parent' => $parent ? (object) $parent : null,
];
}
/**
* Increment the top loop's indices.
*
* @return void
*/
public function incrementLoopIndices()
{
$loop = $this->loopsStack[$index = count($this->loopsStack) - 1];
$this->loopsStack[$index] = array_merge($this->loopsStack[$index], [
//'iteration' => $loop['iteration'] + 1,
'letter' => $loop['iteration'] + 1,
//'index' => $loop['iteration'],
//'first' => $loop['iteration'] == 0,
//'odd' => ! $loop['odd'],
//'even' => ! $loop['even'],
//'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null,
//'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null,
]);
}
/**
* Pop a loop from the top of the loop stack.
*
* @return void
*/
public function popLoop()
{
array_pop($this->loopsStack);
}
/**
* Get an instance of the last loop in the stack.
*
* @return \stdClass|null
*/
public function getLastLoop()
{
if ($last = Arr::last($this->loopsStack)) {
return (object) $last;
}
}
/**
* Get the entire loop stack.
*
* @return array
*/
public function getLoopStack()
{
return $this->loopsStack;
}
}
It gives this error: A non-numeric value encountered I was thinking if we can get the index to increment as a String it'd be worth putting in a PR on github.
Here's the latest one I saw: https://github.com/laravel/framework/pull/27883/commits/c443c83c11a3ba93867e7fa0a2b31af026032a59
Wouldn't {{ $loop->letter }} be awesome?
Please or to participate in this conversation.