1

So I have many forms which use a state dropdown - found this which works great:

                    {!! Form::select('state',array(
'AL'=>'Alabama',
'AK'=>'Alaska',
'AZ'=>'Arizona',
'AR'=>'Arkansas',
'CA'=>'California',
'CO'=>'Colorado',
'CT'=>'Connecticut',
'DE'=>'Delaware',
'DC'=>'District of Columbia',
'FL'=>'Florida',
'GA'=>'Georgia',
'HI'=>'Hawaii',
'ID'=>'Idaho',
'IL'=>'Illinois',
'IN'=>'Indiana',
'IA'=>'Iowa',
'KS'=>'Kansas',
'KY'=>'Kentucky',
'LA'=>'Louisiana',
'ME'=>'Maine',
'MD'=>'Maryland',
'MA'=>'Massachusetts',
'MI'=>'Michigan',
'MN'=>'Minnesota',
'MS'=>'Mississippi',
'MO'=>'Missouri',
'MT'=>'Montana',
'NE'=>'Nebraska',
'NV'=>'Nevada',
'NH'=>'New Hampshire',
'NJ'=>'New Jersey',
'NM'=>'New Mexico',
'NY'=>'New York',
'NC'=>'North Carolina',
'ND'=>'North Dakota',
'OH'=>'Ohio',
'OK'=>'Oklahoma',
'OR'=>'Oregon',
'PA'=>'Pennsylvania',
'RI'=>'Rhode Island',
'SC'=>'South Carolina',
'SD'=>'South Dakota',
'TN'=>'Tennessee',
'TX'=>'Texas',
'UT'=>'Utah',
'VT'=>'Vermont',
'VA'=>'Virginia',
'WA'=>'Washington',
'WV'=>'West Virginia',
'WI'=>'Wisconsin',
'WY'=>'Wyoming',
) ,
Input::old('state'),
array(
'class'       => 'zip-lookup-field-state-short'
))
!!} 

But this appears on several forms throughout the site - how do I put that states array somewhere and include it in a blade view? I've tried just putting it in an array $states = 'Al'=>'Alabama'...etc and using @include, but that doesn't seem to work. Tried something with config but no dice there. Maybe I'm searching wrong but I can't seem to find this...

SO is saying this question: Laravel: Where to store global arrays data and constants? is the same - but none of those soutions are working for a select box...

Community
  • 1
  • 1
DrewM
  • 353
  • 1
  • 3
  • 9
  • 1
    Possible duplicate of http://stackoverflow.com/q/26854030/5045201 – Sapnesh Naik May 03 '17 at 02:19
  • Possible duplicate of [Laravel: Where to store global arrays data and constants?](http://stackoverflow.com/questions/26854030/laravel-where-to-store-global-arrays-data-and-constants) – Bagus Tesa May 03 '17 at 02:49

2 Answers2

0

I like to have my own helper file for these types of things.

app
| -- helpers.php

Then in composer.json find the files: block and add this:

['app/helpers.php']

Then you can put the states array in there

if (!function_exists('us_states')) {
    function us_states() {
        return [
            'AL'=>'Alabama',
            'AK'=>'Alaska',
            'AZ'=>'Arizona',
            'AR'=>'Arkansas',
            'CA'=>'California',
            'CO'=>'Colorado',
            'CT'=>'Connecticut',
            'DE'=>'Delaware',
            'DC'=>'District of Columbia',
            'FL'=>'Florida',
            'GA'=>'Georgia',
            'HI'=>'Hawaii',
            'ID'=>'Idaho',
            'IL'=>'Illinois',
            'IN'=>'Indiana',
            'IA'=>'Iowa',
            'KS'=>'Kansas',
            'KY'=>'Kentucky',
            'LA'=>'Louisiana',
            'ME'=>'Maine',
            'MD'=>'Maryland',
            'MA'=>'Massachusetts',
            'MI'=>'Michigan',
            'MN'=>'Minnesota',
            'MS'=>'Mississippi',
            'MO'=>'Missouri',
            'MT'=>'Montana',
            'NE'=>'Nebraska',
            'NV'=>'Nevada',
            'NH'=>'New Hampshire',
            'NJ'=>'New Jersey',
            'NM'=>'New Mexico',
            'NY'=>'New York',
            'NC'=>'North Carolina',
            'ND'=>'North Dakota',
            'OH'=>'Ohio',
            'OK'=>'Oklahoma',
            'OR'=>'Oregon',
            'PA'=>'Pennsylvania',
            'RI'=>'Rhode Island',
            'SC'=>'South Carolina',
            'SD'=>'South Dakota',
            'TN'=>'Tennessee',
            'TX'=>'Texas',
            'UT'=>'Utah',
            'VT'=>'Vermont',
            'VA'=>'Virginia',
            'WA'=>'Washington',
            'WV'=>'West Virginia',
            'WI'=>'Wisconsin',
            'WY'=>'Wyoming',
        ];
    }
}

Then you can call us_states().

If you are getting errors, make sure you php artisan optimize or composer dump-autoload.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • Using a `helpers` file is always a code smell to me. It would be much better, and truly OOP to use a [view composer](https://laravel.com/docs/5.4/views#view-composers). – jfadich May 03 '17 at 17:42
0

Put them into a key/values table so that they can be edited at some point by using database edits and not hard coding things. What if your application has to work outside of the USA in a country that has different state names?

e.g. something like this: https://github.com/delatbabel/keylists

The key function is this one: Keyvalue::getKeyvaluesByKeyType()

e.g. Keyvalue::getKeyvaluesByKeyType('usa_states')

delatbabel
  • 3,601
  • 24
  • 29