0

I am sure there is a similar question, but I am unsure what to search to find my answer (If you know what I am looking for, please just comment with the link or whatever you do). I need a way to pull all the countries allowed on a particular page (Just the name column in DB) and put it in an array. example:(United States, Canada, New Zealand, etc..) but in the database I have it set up when the pages are created to insert in the DB an array of the id #'s (id column in countries table) works perfectly fine inserts as (1,22,30, etc) 0 is not a countries table row, but I use 0 as the country ID for All Countries. I am just unsure where to start or how to "foreach" this. Here is a little bit of what I am trying to achieve in my Laravel controller:

public function view(Page $page, Request $request)
    {
        // $page->countries = array of country id's (1,12,14,etc)
        // How do i retrieve the country names
        $ip = $request->ip();
        $geo_info = \Location::get($ip);
        $geo_country = $geo_info->country;
        $countries = Country = ??
            
            // How do I add the country names to an array (United States, Mexico, etc)
            // instead of the country id's while keeping the id's so its smaller data in the column for the page countries
        if(in_array($geo_country, $countries)) {
            return view();
        } else {
            return 'Country not allowed.';
        }
    }

How exactly would I format to pull the countries by the IDs and then attach only the country's names column together in an array with ", "?

Sorry if this post is confusing, I am honestly confused myself.

  • What does this return `$geo_country = $geo_info->country;` the name or country id? – Zesty Dec 11 '22 at 21:53
  • The country name, of the given user accessing the page. – Jarrod Estepp Dec 11 '22 at 21:53
  • 1
    Hard to understand both what you have and what you are trying to do. "*pull all the countries allowed on a particular page (Just the name column in DB)*" - does this mean you have a DB table (and maybe model) with country ID and country name? Are you just looking for a Laravel `WHERE IN` query? `$countries = Country::whereIn('id', $page->countries)->get();` – Don't Panic Dec 11 '22 at 22:05
  • @Don'tPanic - I think you may have answered my question on how to collect the countries the easiest way. Yes, I have a Country table with Id and name columns. – Jarrod Estepp Dec 11 '22 at 22:10
  • @Don'tPanic - Love the username lol – Jarrod Estepp Dec 11 '22 at 22:10

2 Answers2

1

Assuming you have a Country model and corresponding table with country ID and country name, I think you are looking for a simple WHERE IN query, eg:

// $page->countries = string list of country ids "1,2,3 ..."
// Let's convert that to an array and use is in our query
$allowed_countries = explode(',', $page->countries);
$countries = Country::whereIn('id', $allowed_countries)->get();

That should give you a Collection of your allowed countries. Now to check if $geo_country is in that collection (assuming the country name is in a field called name), you can use the contains() Collection method:

if ($countries->pluck('name')->contains($geo_country)) {
    return view();
} else {
    return 'Country not allowed.';
}
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • Then i just do a foreach() of the $countries after that to add it into an array for only the country name? – Jarrod Estepp Dec 11 '22 at 22:17
  • When I do this: $countries = Country::whereIn('id', array($page->countries))->get(); it only returns the country United States (The first country in the array) instead of each of the countries. I did a foreach $countries as $country and dd($country->name) – Jarrod Estepp Dec 11 '22 at 22:32
  • 1
    That's not the code in my answer. You said `$page->countries` is already an array, so don't cast it to an array again with `array($page->countries)`. – Don't Panic Dec 11 '22 at 22:35
  • $page->countries is essentially a string in the database "1,2,3". If I attempt to do it that way, it throws an error that a string was given when expecting array. So thats when i added the array() around the $page->countries. – Jarrod Estepp Dec 11 '22 at 22:37
  • But now i am only getting the first country in the list. – Jarrod Estepp Dec 11 '22 at 22:37
  • The dd($countries); is here: https://prnt.sc/3__RAj4Rhfo9 – Jarrod Estepp Dec 11 '22 at 22:39
  • OK ... a string is not an array :-). Updated. – Don't Panic Dec 11 '22 at 22:41
0

Could probably do something like this. Just create a empty array, loop then set the persisting data to that array. Hopefully this gives you an idea.

$countries = array();
foreach($geo_country as $countries) {
 $countries[] = $geo_country;
}
Zesty
  • 273
  • 2
  • 6
  • 19