0

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>
8
  • Which version of Laravel are you using? Commented Aug 16, 2018 at 13:36
  • @IlGala, using 5.6 Commented Aug 16, 2018 at 13:37
  • 1
    What's your DB query looking like? Show us an example of your table structure and efforts you've made to send data to the view. Commented Aug 16, 2018 at 13:38
  • @Adam, I have edited the answer please check out I am trying to use pluck but it shows error of Argument 4 passed to Collective\Html\FormBuilder::select() must be of the type array, null given, called in C:\xampp\htdocs\TestingTask\vendor\laravel\framework Commented Aug 16, 2018 at 13:54
  • You still need to chain a first(), findOrFail(), firstOrFail(), get() after pluck(). Commented Aug 16, 2018 at 14:03

2 Answers 2

4

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 --}}
Sign up to request clarification or add additional context in comments.

Comments

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 -

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.