How to add a placeholder to a select element in Laravel 5

This is my form, I want to add a placeholder to the select element. How can I do that?

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

Upvotes: 3

Views: 7785

Answers (6)

Steve NDENDE
Steve NDENDE

Reputation: 19

Do the following with Laravel 8.+

//In Controller    
$categories = Category::select('id', 'name')->get()->prepend(['name'=>'Choose category','id'=>0])->pluck('name', 'id');

{{-- In your blade file --}}
{!! Form::select("category", $categories, 0, ['required','class'=>'form-control']) !!}

Upvotes: 0

emmanuel
emmanuel

Reputation: 11

lets say your code is as below

{!! Form::text('name',null['class' => 'form-control','placeholder'=>'Select Region']) !!}

just add a comma after NULL since a comma is expected before [

so this is what is expected

{!! Form::text('name',null,['class' => 'form-control','placeholder'=>'Select Region']) !!}

Upvotes: 1

Hadi
Hadi

Reputation: 2905

To create a dropdown list in laravel form builder, code should be like this -

In 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()

and in view -

{!! Form::select('cat_id', $categories, old('cat_id')) !!}

Tested with Laravel 5.x.

Or if you have array like -

$array = ['1' => 'lorem ipsum', '4' => 'Another text'];

And after passing this array to view -

{!! Form::select('cat_id', $array, old('cat_id')) !!}

There will be no placeholder. If you pass below array -

$array = ['' => 'Select category' '1' => 'lorem ipsum category', '4' => 'Another category'];

or there is a collection which you want to pass in view to build select / dropdown list then

$array = $collection->prepend('Select a category', '')->toArray();

You need to pass array in order to build dropdown list.

Note: array_unshift or array_merge will not work as expected!

Upvotes: 4

Tarus
Tarus

Reputation: 136

Try it this way...

 <!-- Select Region Name -->
            <div class="form-group">

 {!! Form::label('region', 'Region:', ['class' => 'col-sm-3 control-label']) !!}
                <div class="col-sm-6">

{!! Form::select('region', $regions,null,['required', 'class' => 'form-control','placeholder'=>'Select Region']) !!}

 </div>

 </div>  

It worked pretty well for me, with my placeholder in place

Upvotes: 1

aldrin27
aldrin27

Reputation: 3407

There's no placeholder in select just a default value

{!! Form::select('jobfornow_category_Id', 
      $category_result = array('' => 'Please Select') + $category_result,
   array('class' => 'form-control')) !!}

Or try to use array_merge:

 {!!
   Form::select(
      'myselect',
       array_merge(['' => 'Please Select'], $category_result),
       $selected_category_result,
         array(
           'class' => 'form-control',
           'id' => 'myselect'
         )
  !!}

Upvotes: 2

RaMeSh
RaMeSh

Reputation: 3424

The HTML select element has no placeholder attribute (only input does), so even if it's in the HTML for your input, it won't do anything.

Upvotes: 3

Related Questions