Open goper-leo opened 7 years ago
I was trying to do something similar, but I found that $country->contains('states')
doesn't work, because the hydrated
array key also contains a value called states
, so in my case $country->contains('states')
always returns true.
To get around the issue, I accessed the $country['states']
like a normal array, and that seems more reliable.
Route::get('/test', function () {
$countries = PragmaRX\Countries\Facade::all();
foreach ($countries as $country) {
if(empty($country['states'])) {
// All the countries with no
printf('<p>No states: %s</p>', $country->name->common);
} else {
// All the countries with states
printf('<p>Has states: %s</p>', $country->name->common);
foreach ($country['states'] as $state) {
var_dump($state);
}
}
}
});
I will get all of country state using this code
$countries = Countries::all();
I loop all of the countries and I've usedcontains
to determine that$country
have no states.So here I found out
Antigua and Barbuda
have no state it thatif condition
cathes it but on the "Republic of Kosovo" , "Republic of Serbia", "Republic of South Sudan" it throws error. because it passes theif else
condition even that countries have no state. I dump the results but it is the same to "Antigua and Barbuda"states => null
. Here is my code: AG XK RS SS - these countries have no states that throwing error except AG