user10665294
user10665294

Reputation:

Laravel Populate Drop-down list of countries using Form Collective using array

Im new to laravel and I want to populate my dropdown list using Form Collectives in Laravel

Fore example, Here is my array of countries

<?php 
    $countries = array("AF" => "Afghanistan",
    "AL" => "Albania",
    "DZ" => "Algeria",
    "AS" => "American Samoa",
    "AD" => "Andorra",
    "AO" => "Angola")
?>

Here is my Form-Collective "Select / Dropdown"

Using Form Collective

   {{Form::select('country',  '', null, ['class' => 'form-control', 'placeholder' => 'Select Country...'])}}

So how can i do it? Anyone who would like to help me, I appreciate it thank you!

Upvotes: 0

Views: 1115

Answers (1)

nakov
nakov

Reputation: 14268

The second parameter in the Form collective select function receives an array of values that you want to be displayed, so simply pass your array, and change {{ with {!! which escapes the HTML output instead of printing it out as text.

   {!! Form::select('country',  $countries, null, ['class' => 'form-control', 'placeholder' => 'Select Country...']) !!}

--- EDIT

If you don't have an admin panel from which you enter a country, then the simplest way with which I will go is store the countries in a language file. For example:

in resources/lang/en/countries.php

return [
   "AF" => "Afghanistan",
   "AL" => "Albania",
   "DZ" => "Algeria",
   "AS" => "American Samoa",
   "AD" => "Andorra",
   "AO" => "Angola"
];

then in your view:

{!! Form::select('country',  trans('countries'), null, ['class' => 'form-control', 'placeholder' => 'Select Country...']) !!}

Upvotes: 2

Related Questions