Right now I have a loop function to work until there are no matches in my database so that I could get a unique string. However, for some reason, my loop is infinite. Is there something I'm doing wrong here?
do {
$testvar = Str::random(5);
$data = User::where('password_url', 'LIKE', '%'.$testvar.'%')->get();
}
while (!empty($data));
The thing is that I do not get any error messages, but I dont get any results as well. Just to reiterate, if my array, $data is empty, I want this loop to be terminated, but if it exists, I want it to continue until it's empty.
The while loop checks the condition first if true then the code it will be executed. If evaluated to false, the execution will be terminated. The while loop iteration value of the current array element is assigned to $value and the array pointer is moved by one until reaches the last array element.
The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it ...
The PHP do...while LoopThe do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.
Try using $data->count()
to check if anything were returned in the result set:
do {
$testvar = Str::random(5);
$data = User::where('password_url', 'LIKE', '%'.$testvar.'%')->get();
}
while ($data->count());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With