我在Laravel 5.4中有一个小型形式,该形式具有复选框和文本框。问题是,当我发布表单时,复选框值并未通过请求。我在复选框上有自定义样式,但肯定不是吗?
我一直在看一段时间,一切看起来正常。我的代码如下:
<form method="post" action="{{ route('admin.settings.save') }}">
{{ csrf_field() }}
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label><b>Site Name</b></label>
<p>This is the name of your LaravelFileManager instance.</p>
<input name="siteName" id="siteName" class="form-control" value="{{ AppHelpersConfigHelper::getValue('site_name') }}" />
</div>
<div class="form-group">
<label><b>Footer Message</b></label>
<p>You can customise the footer message for the application.</p>
<div class="checkbox">
<label>
<input type="checkbox" name="showFooter" id="showFooter" checked="{{ AppHelpersConfigHelper::getValue('show_footer_message') }}"> Show footer message
</label>
</div>
</div>
<button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Save Changes</button>
</div>
</div>
</form>
我的控制器代码就是这样:
public function saveSettings(Request $request) {
$siteName = $request->input('siteName');
$showFooter = $request->input('showFooter');
ConfigHelper::setValue('site_name', $siteName);
ConfigHelper::setValue('show_footer_message', $showFooter);
return redirect()->route('admin.settings')->with('result', 'Settings saved.');
}
我的路线:
Route::post('settings/save', ['uses' => 'AdminSettingsController@saveSettings'])->name('admin.settings.save');
我还对$请求变量完成了一个vardump,甚至缺少复选框值:
array(2) {
["_token"]=> string(40) "sgyO7Kkz1ljsYEZ1G5nkj4uVbmFZqiTMbpK9P6Bi"
["siteName"]=> string(16) "File Manager 1.0"
}
它缺少" Showfooter"变量。
不太确定该在哪里。任何帮助。
,所以我最终得到了工作。使用评论的帮助:
public function saveSettings(Request $request) {
$siteName = $request->input('siteName');
$showFooter = $request->has('showFooter');
ConfigHelper::setValue('site_name', $siteName);
ConfigHelper::setValue('show_footer_message', $showFooter);
return redirect()->route('admin.settings')->with('result', 'Settings saved.');
}
由于某种原因,使用$request->input('showFooter')
无法正常工作。$request->get('showFooter')
在TRUE时会带来结果,因此添加三元物使其每次都起作用。