【问题标题】:Laravel Populate Drop-down list of countries using Form Collective using arrayLaravel 使用数组填充使用 Form Collective 的国家/地区下拉列表
【发布时间】:2018-11-16 23:21:30
【问题描述】:

我是 laravel 新手,我想在 Laravel 中使用 Form Collectives 填充我的下拉列表

例如,这是我的国家/地区数组

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

这是我的表单集合“选择/下拉”

使用表单集合

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

那么我该怎么做呢?谁愿意帮助我,谢谢!

【问题讨论】:

    标签: php arrays laravel drop-down-menu


    【解决方案1】:

    Form 集合选择函数中的第二个参数接收您想要显示的值数组,因此只需传递您的数组,然后将{{ 更改为{!!,它会转义 HTML 输出而不是将其打印为文本。

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

    --- 编辑

    如果您没有用于输入国家/地区的管理面板,那么我将采用的最简单方法是将国家/地区存储在语言文件中。例如:

    resources/lang/en/countries.php

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

    那么在你看来:

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

    【讨论】:

    • 它不起作用:(。我认为有问题或需要添加一些东西
    • @SummerWinter 你是如何传递数组的,或者你得到了什么错误?你有没有试过内联,如果它可以工作,比如这个{!! Form::select('country', ["AF" =&gt; "Afghanistan", "AL" =&gt; "Albania"], null, ['class' =&gt; 'form-control', 'placeholder' =&gt; 'Select Country...']) !!}
    • 是的,它就是这样工作的......但我只是想知道是否有一种像最简单实用的方式那样生成或存储数组的捷径
    • 它确实有效!感谢您帮助像我这样的初学者
    猜你喜欢
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2022-12-03
    • 2010-11-14
    • 1970-01-01
    • 2013-06-28
    相关资源
    最近更新 更多