Skip to content
Advertisement

Laravel: Display data in DropDown from database

I was developing a project with Laravel and want to use dropdownlist and populate it with data from database please help. Here is the code that I use:

Controller:

 public function create()
 {
    $items = Income::pluck('name', 'id');
    return view ('IncomeExpense.create');
 }

View:

 <div class="form-group">
     {{Form::label('', 'Category')}}
     {{Form::select('IncomeExpense',null, $items,['class'=>'form-control',
     'placeholder'=>'Choose category'])}}
 </div>

Advertisement

Answer

First of all,

you have an error in your controller… Please read the eloquent official documentation It should be:

public function create()
 {
    $items = Income::all();
    return view ('IncomeExpense.create', compact('items'));
 }

Anyway you should not use a form builder, I think it’s much more usefull if you use the native blade features like Components & Slots. Form Builder have been removed from Laravel 5.0 if I rember correctly in order to focus the framework attention to the backend…

Heare a couple of example with Bootstrap 4:

Vanilla

<form method="POST" action="{{ route('your-route') }}" aria-label="{{ __('My form') }}">
    @csrf

    <div class="form-group row">
        <label for="dropdown" class="col-sm-4 col-form-label text-md-right">{{ __('My dropdown') }}</label>

        <div class="col-md-12">
            <select class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="dropdown">
              @foreach($my_collection as $item)
              <option value="{{ $item->id }}">{{ $item->text }}</option>
              @endforeach
            </select>

            @if ($errors->has('dropdown'))
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $errors->first('dropdown') }}</strong>
                </span>
            @endif
        </div>
    </div>

{{-- other fields --}}

Components

<!-- /resources/views/components/select.blade.php -->

{{ $label }}

<div class="col-md-{{ $column == null ? 12 : $column  }}">
    <select class="form-control{{ ' ' . $select_css }}{{ $error == null ? '' : ' is-invalid' }}" name="dropdown">
      @foreach($my_collection as $item)
      <option value="{{ $item->id }}">{{ $item->text }}</option>
      @endforeach
    </select>

    @if ($error != null)
        <span class="invalid-feedback" role="alert">
            <strong>{{ $error</strong>
        </span>
    @endif
</div>


<!-- /resources/views/my/form.blade.php -->

<form method="POST" action="{{ route('your-route') }}" aria-label="{{ __('My form') }}">
    @csrf

    <div class="form-group row">
        @component('components.select', ['select_css' => 'whatever', 'my_collection' => $my_collection])
        @slot('label')
        <label for="dropdown" class="col-sm-4 col-form-label text-md-right">{{ __('My dropdown') }}</label>
        @endslot
    </div>

{{-- other fields --}}

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement