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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

Mirasan – You need to pass the view your information retrieved from the database.

$items = Income::pluck('name', 'id');
return view ('IncomeExpense.create')->with(compact('items'));

Then inside your blade file you will access the array ‘items’ (rather than $items).

Regards –


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x