查询laravel中的下拉列表?

时间:2016-02-12 12:57:33

标签: laravel laravel-4 laravel-5

我在Google上搜索过它,但找不到laravel下拉列表的查询。

请给我解决方案,欢迎教程和代码示例。

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

{!! Form::select('userCountry',  \App\Libraries\Countries::countryList('alpha3', 'country_name') , 'USA' , ['class' => 'form-control']) !!}

这里有足够的国家课程让你弄清楚它是如何运作的:

 /**
 * Builds a key/value pair
 * @param $key
 * @param $value
 * @return array|bool
 */
public static function countryList($key, $value)
{
    $json = self::countryData();
    $data = json_decode(self::countryData());

    if(!$data && !is_array($data)) {
        return false;
    }

    $out = [];

    foreach($data as $country) {
        if(property_exists($country, $key) && property_exists($country, $value)) {
            $out[$country->{$key}] = $country->{$value};
        }
    }

    return $out;
}

public static function countryData()
{
    return str_replace(' ', '', str_replace("\n", '', '
    [
        {
            "alpha2": "AF",
            "alpha3": "AFG",
            "country_code": "93",
            "country_name": "Afghanistan",
            "mobile_begin_with": [
                "7"
            ],
            "phone_number_lengths": [
                9
            ]
        },
        {
            "alpha2": "AL",
            "alpha3": "ALB",
            "country_code": "355",
            "country_name": "Albania",
            "mobile_begin_with": [
                "6"
            ],
            "phone_number_lengths": [
                8
            ]
        },
        {
            "alpha2": "DZ",
            "alpha3": "DZA",
            "country_code": "213",
            "country_name": "Algeria",
            "mobile_begin_with": [
                "5",
                "6",
                "7"
            ],
            "phone_number_lengths": [
                9
            ]
        },

答案 2 :(得分:0)

我在使用CustomerModel作为客户详细信息的模型,因为我得到了下拉列表的客户名称:

 $cust_details=CustomerModel::where('active_status',0)->get();
return view('FrontPage.itemadd')->with('cust_details',$cust_details);
在FrontPage.itemadd.blade中的

我使用以下代码将客户名称放在下拉列表中。

 <select id="cab_types" class="form-control" name="customer_name" id="customer_name" required>
 <option value="">Select</option>
    @foreach($cust_details as $cust)
    <option value="{{$cust->id}}">{{$cust->customer_name}}</option>
        @endforeach
          </select>