您可以创建自定义宏来实现这一点。通过代码检查以下步骤:
1)在下创建宏 app/lib/macro.php
<?php
//My custom macro...
Form::macro('mySelect', function($name, $list = array(), $selected = null, $disabled = null, $options = array())
{
$selected = $this->getValueAttribute($name, $selected);
$disabled = $this->getValueAttribute($name, $disabled);
$options['id'] = $this->getIdAttribute($name, $options);
if ( ! isset($options['name'])) $options['name'] = $name;
$html = array();
foreach ($list as $list_el)
{
$selectedAttribute = $this->getSelectedValue($list_el['id'], $selected);
$disabledAttribute = $this->getSelectedValue($list_el['id'], $disabled);
$option_attr = array('value' => e($list_el['id']), 'selected' => $selectedAttribute, 'disabled' => $disabledAttribute);
$html[] = '<option'.$this->html->attributes($option_attr).'>'.e($list_el['value']).'</option>';
}
$options = $this->html->attributes($options);
$list = implode('', $html);
return "<select{$options}>{$list}</select>";
});
2) 将宏注册到 app/Providers/MacroServiceProvider.php
<?php
namespace App\Providers;
use App\Services\Macros\Macros;
use Collective\Html\HtmlServiceProvider;
/**
* Class MacroServiceProvider
* @package App\Providers
*/
class MacroServiceProvider extends HtmlServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
require base_path() . '/app/lib/macro.php';
}
.
.
.
}
3)使用我的自定义宏:
{!!Form::mySelect('category',array(array('id' => '0', 'value'=>'-- Select a category --'), array('id' => '1', 'value'=>'My value1'), array('id' => '2', 'value'=>'My value2')), 0, 0) !!}
用 Laravel 5.2 测试。