I'm using Laravel 8 and PHP 8.
I have a variable that I defined in the global scope and assigned null
value.
I am using laravel's collection's each
function to iterate over each element. Inside this closure I have another closure which is collection's filter
function. I am assigning the output of filter function
into global variable and it is working but inside the closure of each
function only. (checked using xdebug)
Outside the closure the variable becomes null
again even though I passed the variable inside closure using use()
function but still it's not working.
$filtered = null;
$product->each(function ($price) use($coupons, $filtered) {
$filtered = $coupons->filter(function ($coupon) use($price) {
if (!empty($price->iso_code)) {
if ($coupon->iso_code = $price->iso_code) {
$a = $price->currency_code;
$b = $coupon->currency_code;
return $a == $b;
}
}
return $price->currency_code = $coupon->currency_code;
});
});
return $filtered; //null
I have referred this answer & this also but no luck.
Can someone please guide me how can I assign the output of filter
function to global variable?