Laravel:在数据库中的DropDown中显示数据 [英] Laravel: Display data in DropDown from database

查看:115
本文介绍了Laravel:在数据库中的DropDown中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Laravel合作开发一个项目,希望使用dropdownlist并使用数据库中的数据填充它,请提供帮助.这是我使用的代码:

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:

控制器:

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

查看:

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

推荐答案

首先,

您的控制器有错误...请阅读雄辩的官方文档应该是:

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

在Bootstrap 4中有两个示例:

Heare a couple of example with Bootstrap 4:

香草

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

组件

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

这篇关于Laravel:在数据库中的DropDown中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆