Laravel Form Facade For Select 在一个选项上有额外的标签
Laravel Form Facade For Select with extra Tag on one option
我有一个 select html 形式的 select。
<select name="position" id="position" class='position metadata form-control' data-parsley-required data-parsley-required-message="Please select the one position">
<option value="">Select Position</option>
{{ $options = App\Metadata::all()->where('category','position') }}
@foreach($options as $option)
<option value="{{$option->item}}">{{$option->item}}</option>
@endforeach
<option value="1" mytag='position'>Define New</option>
</select>
像这样工作的表单门面
$options = App\Metadata::where('category', 'department')->orderBy('item')->pluck('item', 'item');
$options->prepend('Define New', '1');
$options->prepend('Select department', '0');
?>
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
问题是如何添加 mytag $options->prepend('Define New', '1');到形式 facade
中 select 的最后一个选项
使用 push()
而不是前置。
push()
The push method appends an item to the end of the collection.
您的代码:
$options = App\Metadata::where('category', 'department')->orderBy('item')->pluck('item', 'item');
$options->push('Define New', '1');
$options->push('Select department', '0');
?>
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
作为额外的定义变量,进行查询、调用模型或在视图上应用复杂的逻辑是非常糟糕的做法,应该在控制器本身上完成。
向 Laravel 的 collection 添加其他元素:
你应该在你的控制器中这样做:
$metaDataOptions = App\MetaData::orderBy('item')->pluck('item');
//This will be the first option
$metaDataOptions->prepend('Select department', '0');
//This will be the last option
$metaDataOptions->push('Define New', '1');
然后 return 您对 $metaDataOptions
的看法:
return view('insert_your_view')->withOptions($metaDataOptions);
并且在您看来将 $options
传递给 Form::select()
:
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
向选项添加自定义属性:
您需要将自己的 "type"
个元素定义为 Form
。这可以通过使用 Form::macro
来实现:https://laravelcollective.com/docs/5.2/html#custom-macros
我有一个 select html 形式的 select。
<select name="position" id="position" class='position metadata form-control' data-parsley-required data-parsley-required-message="Please select the one position">
<option value="">Select Position</option>
{{ $options = App\Metadata::all()->where('category','position') }}
@foreach($options as $option)
<option value="{{$option->item}}">{{$option->item}}</option>
@endforeach
<option value="1" mytag='position'>Define New</option>
</select>
像这样工作的表单门面
$options = App\Metadata::where('category', 'department')->orderBy('item')->pluck('item', 'item');
$options->prepend('Define New', '1');
$options->prepend('Select department', '0');
?>
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
问题是如何添加 mytag $options->prepend('Define New', '1');到形式 facade
中 select 的最后一个选项使用 push()
而不是前置。
push()
The push method appends an item to the end of the collection.
您的代码:
$options = App\Metadata::where('category', 'department')->orderBy('item')->pluck('item', 'item');
$options->push('Define New', '1');
$options->push('Select department', '0');
?>
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
作为额外的定义变量,进行查询、调用模型或在视图上应用复杂的逻辑是非常糟糕的做法,应该在控制器本身上完成。
向 Laravel 的 collection 添加其他元素:
你应该在你的控制器中这样做:
$metaDataOptions = App\MetaData::orderBy('item')->pluck('item');
//This will be the first option
$metaDataOptions->prepend('Select department', '0');
//This will be the last option
$metaDataOptions->push('Define New', '1');
然后 return 您对 $metaDataOptions
的看法:
return view('insert_your_view')->withOptions($metaDataOptions);
并且在您看来将 $options
传递给 Form::select()
:
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
向选项添加自定义属性:
您需要将自己的 "type"
个元素定义为 Form
。这可以通过使用 Form::macro
来实现:https://laravelcollective.com/docs/5.2/html#custom-macros