Skip to content
Advertisement

laravel foreach loop in controller

i have a problem about looping data in controller (laravel 4). my code is like this:

JavaScript

when i want to use foreach to loop for $product result with code like this:

JavaScript

the result is returning error Undefined property: IlluminateDatabaseEloquentCollection::$sku

so, i try to improvise a little with this code:

JavaScript

the code returning error like this: Invalid argument supplied for foreach()

is there someone who could help me solve this?

Advertisement

Answer

This will throw an error:

JavaScript

because you cannot loop a model with a specific column ($product->sku) from the table.
So, you must loop on the whole model:

JavaScript

Inside the loop you can retrieve whatever column you want just adding ->[column_name]

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement