In this refactor we show you the power of the blank() and filled() helpers. In this way you can easily determine whether a variable if filled, thus has a value.
$null = null;
$string = "string";
$array = ['a', 'b', 'c'];
$collection = collect($array);
$emptyArray = [];
$number = 1;
$emptyCollection = collect([]);
$boolean = false;
$allVars = get_defined_vars();
$blanks = $filled = $empty = [];
foreach ($allVars as $key => $var) {
if (!isset($var) || (is_countable($var) && count($var) === 0)) {
array_push($blanks, $key);
} else {
array_push($filled, $key);
}
if (empty($var)) {
array_push($empty, $key);
}
}
$output = "Blanks : " . implode(', ', $blanks) . "<br />";
$output .= "Filled : " . implode(', ', $filled) . "<br />";
$output .= "Empty : " . implode(', ', $empty);
$null = null;
$string = "string";
$array = ['a', 'b', 'c'];
$collection = collect($array);
$emptyArray = [];
$number = 1;
$emptyCollection = collect([]);
$boolean = false;
$allVars = get_defined_vars();
/**
* blank() and filled() are two Laravel helpers
*/
$blanks = collect($allVars)->filter('blank');
$filled = collect($allVars)->filter('filled');
$empty = collect($allVars)->filter(fn($var) => empty($var));
$outputRefactored = "Blanks : " . $blanks->keys()->join(', ') . "<br />";
$outputRefactored .= "Filled : " . $filled->keys()->join(',') . "<br />";
$outputRefactored .= "Empty : " . $empty->keys()->join(',');
LLoadout is your loadout for Laravel, helping you kickstart your development process. Besides this refactoring site we also make some cool packages and video tutorials !
Go to LLoadout on github