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

标签 php laravel

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

<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/

相关文章:

Laravel - 将数据传递到 Blade

php - 如何禁用充当提交按钮的图像按钮

php - 如何选择多个文件上传?

php - Laravel 5 全局日期访问器

php - 在 Laravel 4 中创建命名空间

php - 就像在 Laravel 中一样 - 未找到列 :1054

php - 如何从职业解决方案表中的用户那里获取角色?

php - 如何在 javascript 和/或 php 中获取访问者的屏幕分辨率?

php - where 子句中的列不明确 - 这是什么意思?

php - 如何在回显mysql查询结果时忽略重复行