简体   繁体   English

Laravel使用表格集体使用数组填充国家下拉列表

[英]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 我是laravel的新手,我想使用Laravel中的Form Collectives填充下拉列表

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. 这会转义HTML输出,而不是将其打印为文本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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