我的联系表格有问题。除一个字段外,所有字段均为必填字段。通常,我会在迁移中插入可为null的内容,但显然不起作用。我试图使验证中的可为空,但这也不起作用。所以我有点困惑。
public function up()
{
Schema::create('kontaktforms', function (Blueprint $table) {
$table->increments('id');
$table->string('navn');
$table->string('mobilnr');
$table->string('fastnetnr')->nullable();
$table->string('mail');
$table->string('emne');
$table->text('beskrivelse');
$table->timestamps();
});
}
public function store(Request $request)
{
$this->validate($request, [
'navn' => 'required',
'mobil' => 'required',
'email' => 'required',
'emne' => 'required',
'beskrivelse' => 'required'
]);
$kontakt = new Kontaktform([
'navn' => $request['navn'],
'mobilnr' => $request['mobil'],
'fastnetnr' => $request['fastnetnr'],
'mail' => $request['email'],
'emne' => $request['emne'],
'beskrivelse' => $request['beskrivelse']
]);
$kontakt->save();
Session::flash('success', 'Vi har nu modtaget din besked');
return redirect()->route('kontakt.create');
}
表格
<form id="form-contact" action="{{route('kontakt.store')}}" method="POST">
@csrf
<h1 class="display-4">Kontakt os</h1>
<div class="form-group">
<input name="navn" type="text" class="form-control" placeholder="Dit navn...">
</div>
<div class="form-group">
<input name="mobil" type="text" class="form-control" placeholder="Din mobil">
</div>
<div class="form-group">
<input name="fastnetnr" type="text" class="form-control" placeholder="Evt fastnetnr">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" placeholder="Din email">
</div>
<div class="form-group">
<input name="emne" type="text" class="form-control" placeholder="Emne">
</div>
<div class="form-group">
<textarea name="beskrivelse" class="form-control" placeholder="Skriv din besked her" rows="4"></textarea>
</div>
<br>
<input type="submit" class="btn btn-primary btn-block" value="Send">
<hr>
</form>
答案 0 :(得分:2)
将可空字段迁移为
$table->string('fieldname')->nullable();
,并在验证过程中使用 Validator 或 FormRequest 确认您没有添加必需的属性
'fieldname' => 'required|integer'
您必须只有
'fieldname' => 'integer'
答案 1 :(得分:1)
我不确定您要做什么,但是表kontaktforms
中没有试图从控制器输入的名为fastnetnr
的任何字段。
也许在迁移中添加该字段,回滚后再次运行迁移,然后尝试?