I'm new on laravel. Why I'm always get error:
array_map(): Argument #2 should be an array ?
whereas I'm assigning parameter array on this method?
this is my example code :
$products = Category::find(1)->products;
note : 1 category has many products
this is the array from query :
[{
"id": "1",
"name": "action figure",
"created_at": "2015-11-09 05:51:25",
"updated_at": "2015-11-09 05:51:25"
}, {
"id": "2",
"name": "manga",
"created_at": "2015-11-09 05:51:25",
"updated_at": "2015-11-09 05:51:25"
}]
when I'm trying the following code:
$results = array_map( function($prod) {
return $prod.name;
}, $products);
and I get the error like below:
"array_map(): Argument #2 should be an array"
$products
should be an array, first convert your data into an array. also check passed variable is an array or not – Alfanovar_dump
the variable to analyze the variable. I think in your case$products
is an object – Alfano