Nick385's avatar

Illuminate\Html Form Country Drop down

Im creating a form using Illuminate\Html. So i got two questions about it is Illuminate html better? Second how can i create the drop down?

<div class="form-group">
            {!! Form::label('Country', 'Country:') !!}
            @foreach($countries::all() as $country)
            {!! Form::select('country', array($country))!!}
            @endforeach

        </div>
<!-- Country Form Input -->
        <div class="form-group">
            {!! Form::label('Country', 'Country:') !!}
            {!! Form::select('country', $country) !!}
        </div>

the above both don't work

@inject('countries', 'App\Http\Utilities\Country')

@extends('app')

@section('content')

    <h1>Contact</h1>

    <hr/>

    {!! Form::open() !!}
        <!-- Firstname Form Input -->
        <div class="form-group">
            {!! Form::label('firstname', 'Firstname:') !!}
            {!! Form::text('firstname', null, ['class' => 'form-control']) !!}
        </div>

        <!-- Lastname Form Input -->
        <div class="form-group">
            {!! Form::label('lastname', 'Lastname:') !!}
            {!! Form::text('lastname', null, ['class' => 'form-control']) !!}
        </div>
    
        <!-- Email Form Input -->
        <div class="form-group">
            {!! Form::label('email', 'Email:') !!}
            {!! Form::text('email', null, ['class' => 'form-control']) !!}
        </div>

        <!-- Street Form Input -->
        <div class="form-group">
            {!! Form::label('street', 'Street:') !!}
            {!! Form::text('street', null, ['class' => 'form-control']) !!}
        </div>

        <!-- City Form Input -->
        <div class="form-group">
            {!! Form::label('City', 'City:') !!}
            {!! Form::text('City', null, ['class' => 'form-control']) !!}
        </div>

        <!-- Zip Form Input -->
        <div class="form-group">
            {!! Form::label('zip', 'Zip/Postal Code:') !!}
            {!! Form::text('zip', null, ['class' => 'form-control']) !!}
        </div>

        <!-- Country Form Input -->
        <div class="form-group">
            {!! Form::label('Country', 'Country:') !!}
            {!! Form::select('country', $countries ) !!}


        </div>

        <!-- State Form Input -->
        <div class="form-group">
            {!! Form::label('state', 'State:') !!}
            {!! Form::text('state', null, ['class' => 'form-control']) !!}
        </div>

        <!-- Add Contact Form Input -->
        <div class="form-group">
            {!! Form::submit('Add Contact', ['class' => 'btn btn-primary form-control']) !!}
        </div>



    {!! Form::close() !!}

@stop
<?php

namespace App\Http\Utilities;

class Country{

    protected  static $countries = [
      "United States"                                => "us",
      "Afghanistan"                                  => "af",
      "Albania"                                      => "al",
      "Algeria"                                      => "dz",
      "American Samoa"                               => "as",
      "Andorra"                                      => "ad",
      "Angola"                                       => "ad",
      "Anguilla"                                     => "ai",
      "Antarctica"                                   => "aq",
      "Antigua and Barbuda"                          => "ag",
      "Argentina"                                    => "ar",
      "Armenia"                                      => "am",
      "Aruba"                                        => "aw",
      "Australia"                                    => "au",
      "Austria"                                      => "at",
      "Azerbaijan"                                   => "az",
      "Bahamas"                                      => "bs",
      "Bahrain"                                      => "bh",
      "Bangladesh"                                   => "bd",
      "Barbados"                                     => "bb",
      "Belarus"                                      => "by",
      "Belgium"                                      => "be",
      "Belize"                                       => "bz",
      "Benin"                                        => "bj",
      "Virgin Islands (British)"                     => "vg",
      "Virgin Islands (U.S.)"                        => "vi",
      "Wallis and Futuna Islands"                    => "wf",
      "Western Sahara"                               => "eh",
      "Yemen"                                        => "ye",
      "Serbia"                                       => "yu",
      "Zambia"                                       => "zm",
      "Zimbabwe"                                     => "zw"
    ];

    public static function all()
    {
        return array_keys(static::$countries);
    }

}
0 likes
4 replies
Nick385's avatar

@jlrdw Yeah im currently working on this one https://laracasts.com/series/build-project-flyer-with-me/episodes/5

That is where i got stuck. im doing laravel 5 fundamentals Along side https://laracasts.com/series/laravel-5-fundamentals/episodes/10 To build my app But in laravel 5 fundamentals Jeffrey is using Illuminate\Html In the flyer he isn't, i did noticed that laravel 5 fundamentals is older. But i dont know if Illuminate is a better way to go still. I can make the dropdown without illuminate but for learning purpose i want to know how to use it with Illuminate as well.

<!-- Country Form Input -->
        <div class="form-group">
            {!! Form::label('Country', 'Country:') !!}

                <select id="country" name="country" class="form-control">
                @foreach($countries::all() as $country => $code)
                    <option value="{{ $code }}">{{ $country }}</option>
                @endforeach
                </select>
        </div>

This works but i cant get it to work with illuminate

My apologies for my broken english

jlrdw's avatar

I probably wouldn't load all countries at once, to me, that's too many for one drop down, perhaps try Ajax, let user enter the first letter or two of the chosen country, and limit the list to that. Just a suggestion.

Please or to participate in this conversation.