简体   繁体   English

Laravel使用默认值设置创建表单选择

[英]Laravel creating a form select with default value set

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? 没有设置默认值?

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. 此外,请注意,在Firefox中, selected值未正确显示。 To fix this, change your drop-down list to (in Blade): 要解决此问题,请将您的下拉列表更改为(在Blade中):

Form::select('location', $locations_list, $emp_loc, array('autocomplete'=>'off'))

This last part is a weird behavior of Firefox 最后一部分是Firefox的怪异行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM