-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Track a loop variable for sequence and pass it with count to closure #37799
Conversation
So - what is "loop"? I'm not sure you actually explain what you mean by "loop count"? |
@taylorotwell Sorry that the use case is not more clear. For my specific case, I have a model I needed a way to increment the sort for each loop, but currently, there is not a way to know the loop count in the sequence, so we are doing so "extra" code. Here is a very simple example... $sorts = Collection::times(rand(1, 5), fn ($i) => ['sort' => ($i + 1) * 100])->toArray();
$count = count($sorts);
return $this->has(
Section::factory()->count($count)
->sequence(...$sorts),
'recommendations'
)->state(fn () => ['sort' => $sort]); If sequence kept up with the loop count, then we could do... return $this->has(
Section::factory()->count(rand(1, 5))
->sequence(fn($loop) => ['sort' => ($loop +1)*100]),
'recommendations'
); I hope this is more clear. In the end, just looking to optionally know in the sequence closure the number of times that the sequence has looped & how many times it is looping. Thanks again for considering this. |
Why not just make the |
I really like the idea of passing Personally, I would like to repurpose <?php
namespace Illuminate\Database\Eloquent\Factories;
class Sequence
{
/**
* The sequence of return values.
*
* @var array
*/
protected $sequence;
/**
* The count of the sequence items.
*
* @var int
*/
public $count;
/**
* The current index of the sequence iteration.
*
* @var int
*/
public $index = 0;
/**
* Create a new sequence instance.
*
* @param array $sequence
* @return void
*/
public function __construct(...$sequence)
{
$this->sequence = $sequence;
$this->count = count($sequence);
}
/**
* Get the next value in the sequence.
*
* @return mixed
*/
public function __invoke()
{
return tap(value($this->sequence[$this->index % $this->count], $this), function () {
$this->index = $this->index + 1;
});
}
} Then the example above would be... return $this->has(
Section::factory()->count(rand(1, 5))
->sequence(fn(Sequence $sequence) => ['sort' => ($sequence->index + 1) * 100]),
'recommendations'
); I would be happy to do whichever you prefer. |
Since |
Since we are going to expose a public variable, should we consider making the "API" emulate the |
Overkill IMO for now. Nobody asking for it. |
OK, I have refactored the Again, thanks for considering this. |
I have a use case where I would like to know the loop count in a sequence, so I thought that others might like the ability too.
All this does is track a loop variable similar to foreach to use with % to get the index. Then it passes it to the closure.
$loop
&$count
is optional, and people would only declare if needed. I almost did not pass$count
, but there are times that we do a random number in thecount
method, so there is a slight use case.This is a re-mix of #37753 in a way to be backward compatible, as @driesvints suggested.
Thanks for considering & I hope that you see value in it.
Thanks so much for all the work that you put into Laravel!