简体   繁体   中英

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!

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...']) !!}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM