【问题标题】:How to populate database values in dropdwon in laravel如何在 laravel 的下拉列表中填充数据库值
【发布时间】:2017-03-20 18:57:16
【问题描述】:

我想在下拉列表中显示数据库值..我尝试了很多,但它不适合我.. 我收到错误我不知道我在哪里弄错了..我是 laravel 的新手..请帮助我在哪里弄错了..

查看:

<div class="form-group">
<label for="name" class="col-md-4 control-label">User type</label>

<div class="col-md-6">
        <?php
        {!!Form::select('user_type_id[]', $usertypes_list, $thing->user_type->pluck('id'), ['class' => 'form-control'])!!}
        ;?>
</div>

控制器:

public function get() {
    $usertypes_list = User_type::lists('id', 'type');
    return view('edit')->compact('user_type');
}

谁能帮帮我... 提前致谢。

【问题讨论】:

  • 你能试试这个`{!! Form::select('user_type_id[]', $usertypes_list, null, ['class' => 'form-control']) !!}` 并删除&lt;?php ?&gt; 标签
  • 当我尝试找不到此类“表单”时出现此错误
  • 您是否将laravelcollective 添加到您的项目中?
  • laravelcollective 是为了什么..我可以知道目的
  • Form::select 不是 laravel 5+ 中的内置功能,它是一个名为 laravelcollective 的包,它非常常见且有用

标签: mysql laravel-5.4


【解决方案1】:

运行 composer require "laravelcollective/html":"^5.4"laravelcollective 添加到您的项目中,因为 Form::select 在 laravel 5+ 中不再可用

快速浏览this laravelcollective Doc.s

编辑: 在您的控制器中,您必须通过 $usertypes_list 所以函数应该是

public function get() {
    $usertypes_list = User_type::pluck('id', 'type');
    return view('edit')->compact('usertypes_list');
}

那么您的视图应如下所示:

<div class="form-group">
<label for="name" class="col-md-4 control-label">User type</label>

<div class="col-md-6">
        {!!Form::select('user_type_id[]', $usertypes_list, null, ['class' => 'form-control'])!!}
</div>

【讨论】:

  • 当我尝试安装这个时,我得到了这样的结果 ./composer.json 已更新 正在加载包含包信息的作曲家存储库 更新依赖项(包括 require-dev)
  • 尝试运行composer dump-autoload, php artisan view:clear
  • 现在我得到了这个错误 Undefined variable: usertypes_list
  • 我想从用户类型表中获取值
  • 我已经编辑了答案。您将一个不存在的变量传递给视图user_type 并调用$thing-&gt;user_type-&gt;pluck('id'),这对我来说是可以理解的! $thing 是什么?
猜你喜欢
  • 2018-10-03
  • 2017-03-12
  • 2016-01-09
  • 2013-06-20
  • 1970-01-01
  • 2014-08-27
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多