将组合框的当前值或来自数据库的 html select 框

Put the current value of combobox or the html select box from DB

我有这个代码

{{Form::select('area_id', Area::lists('area_description','area_id') ,'',array('class' => 'form-control'))}

如何从数据库中获取 Select 框的当前值。顺便说一句,我正在使用 Laravel 和 Blade 模板。

你可以这样使用:

{{
    Form::select(
        'area_id', 
        Area::lists('area_description','area_id'),
        old('area_id'), // or Input::old('area_id')
        ['class' => 'form-control']
    )
}}

在这种情况下,不要在您的 template 中使用 Area::lists('area_description','area_id'),而是使用从您的控制器传递它,例如(在您的控制器中):

$area_ids = Area::lists('area_description','area_id');

return view('view_name')->with('area_ids', $area_ids);

然后在您的 template 中使用以下内容:

{{ Form::select('area_id', $area_ids, old('area_id'), ['class' => 'form-control']) }}

对于Laravel-5.1.x

The lists method now returns a Collection instance instead of a plain array for Eloquent queries. If you would like to convert the Collection into a plain array, use the all method:

$area_ids = Area::lists('area_description','area_id')->all();

查看 the upgrade guide 了解详情。

我已经解决了我自己的问题wew。 使用此代码:

{{Form::select('area_id', $area_ids , $person->area_id ,array('class' => 'form-control'))}}

忘记应该是id或者option值不是option内容。我指的选项是 <option> 标签。