gpt4 book ai didi

php - 如何在 Laravel 5 中为选择元素添加占位符

转载 作者:行者123 更新时间:2023-12-04 16:14:27 27 4
gpt4 key购买 nike

这是我的表单,我想为选择元素添加一个占位符。我怎样才能做到这一点?

<div class="row">
{!! Form::model(array('method' => 'post','class'=>'post-data','files' => true)) !!}
<div class="col-md-6">
<div class="form-group">
<div class='col-md-4'>
{!! Form::label('Category Name', 'Category Name',array('class' => 'form-label')) !!}
</div>
<div class='col-md-8'>
{!! Form::select('jobfornow_category_Id',$category_result,null,array('class' => 'form-control')) !!}
</div>
</div>

<div class="form-group">
<div class='col-md-4'>
{!! Form::label('Sub Category Name', 'Sub Category Name',array('class' => 'form-label')) !!}
</div>
<div class='col-md-8'>
{!! Form::text('jobfornow_subcategory_Name',null,array('class' => 'form-control')) !!}
</div>
</div>
<div class='col-md-4'>
{!! Form::label('Description', 'Description',array('class' => 'form-label')) !!}
</div>
<div class='col-md-8'>
{!! Form::textarea('jobfornow_subcategory_Description',null,array('class' => 'form-control')) !!}
</div>
<div class="form-group">
<div class='col-md-4'></div>
<div class='col-md-8' style="margin-top: 10px;">
{!!Form::submit('Submit',array('class' => 'btn btn-default btn-cons'))!!}
</div>
</div>
{!! Form::close() !!}
</div>
</div>

最佳答案

要在 laravel 表单生成器中创建下拉列表,代码应该是这样的 -

在 Controller 中 -

$categories = Category::select('id', 'name')->lists('name', 'id')->prepend('Select a category', '')->toArray();

if you are using Laravel 5.3 or above use pluck() instead of lists()



并鉴于 -
{!! Form::select('cat_id', $categories, old('cat_id')) !!}

使用 Laravel 5.x 测试。

或者,如果您有类似的数组 -
$array = ['1' => 'lorem ipsum', '4' => 'Another text'];

在传递这个数组来查看之后 -
{!! Form::select('cat_id', $array, old('cat_id')) !!}

不会有占位符。如果你通过以下数组 -
$array = ['' => 'Select category' '1' => 'lorem ipsum category', '4' => 'Another category'];

或者有一个你想在 View 中传递的集合来构建选择/下拉列表
$array = $collection->prepend('Select a category', '')->toArray();

您需要传递数组才能构建下拉列表。

Note: array_unshift or array_merge will not work as expected!

关于php - 如何在 Laravel 5 中为选择元素添加占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32582131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com