Laravel 创建一个带有默认值集的表单选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16089537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel creating a form select with default value set
提问by Shaun Michael Stone
Controller:
控制器:
$employee = Staff::where('staff_id', '=', $id)->where('user_role', '=','Employee')->first();
$emp_loc = $employee->locations()->pivot()->only('loc_id');
$locations_list = Location::lists('address1', 'loc_id'); //get list of locations
View:
看法:
<!--location -->
<div class="control-group">
<label class="control-label">Location</label>
<div class="controls">
@if(isset($emp_loc))
{{ Form::select('location', $locations_list, $emp_loc) }}
@else
{{ Form::select('location', $locations_list) }}
@endif
</div>
</div>
The third parameter is meant to be the default value for the select box however it always begins at the first value.
第三个参数是选择框的默认值,但它总是从第一个值开始。
Source Code:
源代码:
<div class="control-group">
<label class="control-label">Location</label>
<div class="controls">
<select name="location"><option value="1">Bethel</option><option value="2">Brooklyn</option><option value="3">Germantown</option><option value="4">Memphis</option><option value="5">Brooklyn</option></select> </div>
</div>
No default value set?
没有设置默认值?
回答by clod986
It might be a database configuration problem.
可能是数据库配置问题。
Go back to the migration file and make sure your foreign keys are set properly. This is what I would do to properly set them:
返回迁移文件并确保您的外键设置正确。这就是我要正确设置它们的方法:
Schema::create('pivot', function(Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('loc_id');
$table->foreign('loc_id')
->references('id')->on('location_list');
.....
}
Schema::create('location_list', function(Blueprint $table) {
$table->increments('id');
.....
}
Moreover, pay attention that in Firefox the selected
value is not displayed properly. To fix this, change your drop-down list to (in Blade):
此外,请注意在 Firefox 中该selected
值未正确显示。要解决此问题,请将下拉列表更改为(在 Blade 中):
Form::select('location', $locations_list, $emp_loc, array('autocomplete'=>'off'))
This last part is a weird behavior of Firefox
最后一部分是 Firefox 的一个奇怪的行为