When we work on a PHP based project like WordPress, Laravel or Codeigniter, or core PHP, then we will see this type of error that invalid argument supplied for foreach()” warning occurs. I will show you in this tutorial that how we can solve invalid argument supplied for foreach laravel, wordpress, codeigniter, or php.
Remember that whatever version of PHP we are on, we need to ensure that an array or an object is always passed to foreach
. Let's see the solution:
foreach ($value ?? [] as $item) {
//
}
Or we can use like that:
if (is_iterable($value)) {
foreach ($value as $item) {
…
}
}
Read also: How to Set Default Value in Laravel Eloquent Model?
Hope it can help you.
#laravel #php