【问题标题】:Laravel Collective drop-down to mysql in Laravel 5.8Laravel 5.8 中的 Laravel Collective 下拉到 mysql
【发布时间】:2019-04-15 15:59:53
【问题描述】:

我得到错误:

SQLSTATE[01000]:警告:1265 列“级别”的数据被截断..

我的猜测是 Form::select 应该以某种不同的方式使用,如何?

// in my migration:
$table->enum('level', ['easy', 'hard']);

// in my controller Store function:
$tablee = new Tablee; // this is view file called Tablee.php
$tablee->level = $request->input('level');
$tablee->save();

// and part of my code in create.blade.php
<div class="form-group">
  {{Form::label('level', 'Please choose level')}}
  {{Form::select('level', ['easy' => 'easy', 'hard' => 'hard'], ['class' => 'form-control'])}}
</div>

【问题讨论】:

标签: laravel laravelcollective


【解决方案1】:

Form::select 的第三个参数是被选中的元素。

public function select($name, $list = [], $selected = null, $options = [])

所以你应该改变你的

{{ Form::select('level', ['easy' => 'easy', 'hard' => 'hard'], null, ['class' => 'form-control']) }}

【讨论】:

  • 现在我得到:违反完整性约束:1048 列“级别”不能为空(23000)
  • 您有默认选择吗?问题是因为您没有将 level 设置为任何值。调试您的$request-&gt;input('level') 它是否返回任何内容?确保您的模型的 $fillable 数组中有 level
  • 完整性错误是因为我在 Controller 文件中的拼写错误,所以主要问题已解决,谢谢。我的模型中没有数组,请用代码写下你的意思,你看我是初学者...
  • 如果它有效,那就太好了。我说的是批量分配部分,您可以在文档here 中阅读更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 2016-02-17
  • 1970-01-01
  • 2016-05-22
相关资源
最近更新 更多