Laravel 5.1 - Invalid argument supplied for foreach()
Hello,
I got this error "Invalid argument supplied for foreach()" when I am trying to foreach results
this is a code
$results = Cartalyst\Sentinel\Users\EloquentUser::search('arina');
if (is_array($results)) {
foreach ($results as $result) {
foreach ($result as $item) {
echo ($item["full_name"]);
}
}
}
I am trying to get and display results form Algolia using this package: https://github.com/algolia/algoliasearch-laravel
Can anyone help me ?
And what is the content of the $results or $result variable (depending on which is erroring)?
You can use this to always make it an array in the foreach
foreach ((array) $results as $result) {
//
}
@bashy Thanks for response.
$results containing
array:8 [▼
"hits" => array:3 [▶]
"nbHits" => 3
"page" => 0
"nbPages" => 1
"hitsPerPage" => 20
"processingTimeMS" => 1
"query" => "arina"
"params" => "query=arina"
]
$result containing three arrays:
array:3 [▼
0 => array:20 [▼
"id" => 3
"email" => "arina@arina.com"
"password" => "$2y$10$rG9GCRHQw1WPOjFv4b3qL.QkgF7jFQ8U.q6V7VDuLUw5mK7HNrJzS"
"permissions" => []
"last_login" => array:3 [▶]
"first_name" => "Arina"
"last_name" => "Systems"
"full_name" => "Arina Systems"
"birthDay" => "1996-03-26"
"birthPlace" => "Egypt"
"gender" => "Female"
"address" => "2 El Fawakeh St., next to Mostafa Mahmoud Mosque"
"phone" => "+2010000936782"
"national_id" => "29404118800359"
"idProof" => "system/users/IDs/29404118800357.jpg"
"image" => "system/users/pp/29404118800357.jpg"
"created_at" => "2015-12-22 20:35:37"
"updated_at" => "2016-01-25 14:38:56"
"objectID" => "3"
"_highlightResult" => array:6 [▶]
]
1 => array:20 [▼
"id" => 2
"email" => "salma@arina.com"
"password" => "$2y$10$F8/3Og1nnBoebvB4O7fLHOp.F1txUNmujQ8LSInyK6SNDpqEDaIfm"
"permissions" => []
"last_login" => "2016-01-25 03:20:28"
"first_name" => "Salma"
"last_name" => "ElsaadaNy"
"full_name" => "Salma ElsaadaNy"
"birthDay" => "1996-03-26"
"birthPlace" => "Egypt"
"gender" => "Female"
"address" => "2 El Fawakeh St., next to Mostafa Mahmoud Mosque"
"phone" => "+2010000936781"
"national_id" => "29404118800358"
"idProof" => "system/users/IDs/29404118800357.jpg"
"image" => "system/users/pp/29404118800357.jpg"
"created_at" => "2015-12-22 20:35:36"
"updated_at" => "2016-01-25 03:20:28"
"objectID" => "2"
"_highlightResult" => array:6 [▶]
]
2 => array:20 [▼
"id" => 1
"email" => "zedan@arina.com"
"password" => "$2y$10$33ga9yVODaH96o5hWnD7uOgQ3TaXZW/XH9pPDKxnbY5thO7QBQa7."
"permissions" => []
"last_login" => "2015-12-30 13:30:24"
"first_name" => "Mohamed"
"last_name" => "Zedan"
"full_name" => "Mohamed Zedan"
"birthDay" => "1994-04-11"
"birthPlace" => "Emirates"
"gender" => "Male"
"address" => "2 El Fawakeh St., next to Mostafa Mahmoud Mosque"
"phone" => "+2010000936780"
"national_id" => "29404118800357"
"idProof" => "system/users/IDs/29404118800357.jpg"
"image" => "system/users/pp/29404118800357.jpg"
"created_at" => "2015-12-22 20:35:36"
"updated_at" => "2015-12-30 13:30:24"
"objectID" => "1"
"_highlightResult" => array:6 [▶]
]
]
I need to get for example "full_name" form each array, so i make foreach again. but a got an error above.
Don't you want (change it from object to array if required)
$results = Cartalyst\Sentinel\Users\EloquentUser::search('arina');
foreach ((array) $results->hits as $result) {
echo ($result->full_name);
}
?
@bashy I don't know how i can thank you.. :)
But i just do this
$results = Cartalyst\Sentinel\Users\EloquentUser::search('arina');
foreach ((array) $results['hits'] as $result) {
echo ($result['full_name']);
}
because this is array not object. Thank you very much..
Yeah just modify as required. Most of the time things will be objects, not sure why that's returning an array but enjoy :)
If my reply helped you, mark it as the best answer. I'd appreciate it!
Please or to participate in this conversation.