Repository navigation
Something like Arr::toCssClasses but for dot
#59868
Replies: 3 comments 4 replies
|
because I need to work at multiple same route path with a different route name, so I just make macro like this; public static function toRouteNames($array)
{
$routeList = static::wrap($array);
$names = [];
foreach ($routeList as $name => $constraint) {
if (is_numeric($name)) {
$names[] = $constraint;
} elseif ($constraint) {
$names[] = $name;
}
}
return implode('.', $names);
}I just figure it out from cheers. edit: it was wrong code, sorry. |
|
Nice idea—borrowing the pattern from If the goal is to build dot-notated route names from a mixed array, you could simplify and make it a bit more expressive using collections: public static function toRouteNames($array)
{
return collect(\Illuminate\Support\Arr::wrap($array))
->map(fn ($value, $key) => is_numeric($key) ? $value : ($value ? $key : null))
->filter()
->implode('.');
}This keeps the intent clear and handles both keyed (['foo' => true]) and non-keyed (['bar']) values consistently. It might also be worth defining how you want to handle non-boolean truthy values or nested arrays, depending on how generic you want this helper to be. Overall, neat utility, this pattern is definitely useful for dynamic route naming. |
|
I wonder why you want to pass this in with said shape rather than just a mere list. In what real world scenario would you want to have |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
hi! is there any helpers that can transform from
['foo' => true, 'bar']intofoo.bar?All reactions