KaziBablu
KaziBablu

Reputation: 533

Laravel Dropdown Form Get Form Database

I am using laravel 5.4 . I am trying to get data form Department table and data come also the problem there more dropdown menu come also please tell how can i solve this problem. Thanks advance.[

<div class="form-group">
                        {{Form::label('department','Department')}}
     @foreach($department as $value)
      {{Form::select('department',[$value->name], ' ', array('placeholder' => 'Select Department','class'=>'form-control'))}}
     @endforeach

 </div>

Upvotes: 1

Views: 886

Answers (2)

Jonathon
Jonathon

Reputation: 16283

It's not entirely clear what you're after. Your current code seems to output several select boxes, one for each department and my best guess is that you want to output only one select box, with each department as an option in it. You can achieve that by doing something like this (You may have to adapt this code slightly to suit your needs):

The manual approach

<div class="form-group">
    <label for="department">Department</label>
    <select id="department" name="department" class="form-control">
        <option value="">Select Department</option>
        @foreach ($departments as $department)
            <option value="{{ $department->id }}">{{ $department->name }}</option>
        @endforeach
    </select>
</div>

Using Laravel Collective's Form Builder

<div class="form-group">
    {!! Form::label('department', 'Department') !!}
    {!! Form::select('department', ['' => 'Select Department'] + $departments->pluck'name', 'id')) !!}
</div>

Upvotes: 1

Deepak Kumar
Deepak Kumar

Reputation: 221

Try to do this way :-

<div class="form-group">
  {{Form::label('department','Department')}}
  <select class="form-control" name="department">
    @foreach($department as $value)
      <option value="$value">$value</option>
    @endforeach
  </select>
</div>

Upvotes: 0

Related Questions