I'm trying to compare new string against old string from database, and if that string exists increase it by one, for example if string car exists new string should be car-1 and if that string exist then car-2...
I'm using Laravel, and have achieved that by using while() loop, but I would like to do it with for() loop too.
This is my code that works:
foreach($lang_codes as $key => $value)
{
$oldAlias = str_slug($request->{"new_category_lang_{$value->code}"}, '-');
$newAlias = $oldAlias;
$aliasCheck = ShopCategoryName::where('alias', $oldAlias)->first();
$newCategory = new ShopCategoryName;
$i = 1;
while ($aliasCheck)
{
$newAlias = $oldAlias . '-' . $i;
$aliasCheck = ShopCategoryName::where('alias', $newAlias)->first();
$i++;
}
}
I was trying to use strcmp() with for() loop, but I don't know how to put that code together, it was mess.
So basically, I'm trying code above to work with for() loop, but my brain has blocked and overcomplicated.