从查询生成器结果列表中设置下拉列表的默认值 Laravel 5.1
Set default value of dropdown from a Query Builder result list Laravel 5.1
我正在制作我的表单的编辑页面,我有一个 select 下拉列表,但我需要为该设置 selected 值,因为它是一个编辑表单。我是这样做的:
public function edit($id)
{
$form = Form::find($id);
$agent_options = array('' => 'Choose One') + DB::table('agents')->lists('name','id');
$campaign_options = array('' => 'Choose One') + DB::table('campaigns')->lists('name','id');
$query = "SELECT a.id, a.form_id, a.metrics_id, a.response, b.response as responseoption, b.metrics_name, b.description, b.question, a.remarks FROM qcv.forms_responses a INNER JOIN metrics b ON a.metrics_id = b.id WHERE form_id = $id;";
$metric = DB::connection('mysql')->select($query);
return view('form.edit')->with(array('form'=> $form, 'agent_options' => $agent_options, 'campaign_options' => $campaign_options, 'metric' => $metric));
}
在我看来
<div class="col-md-6">
{!! Form::select('agent_id', $agent_options, '',array('class' => 'form-control', 'id' => 'agent_id', 'required' => 'required')) !!}
</div>
让我们使用我的 $agent_options
下拉菜单,它将在我的代理 table 中拉取代理列表,但在我看来,如何根据代理 ID 设置默认值 selected ?假设
agend_id = 1
默认为 select。
您需要将默认值传递给 select()
as
{!! Form::select('agent_id', $agent_options, $defaultValue, array('class' => 'form-control', 'id' => 'agent_id', 'required' => 'required')) !!}}
将 $defaultValue
替换为您需要的默认值 select。
参考DOC
我正在制作我的表单的编辑页面,我有一个 select 下拉列表,但我需要为该设置 selected 值,因为它是一个编辑表单。我是这样做的:
public function edit($id)
{
$form = Form::find($id);
$agent_options = array('' => 'Choose One') + DB::table('agents')->lists('name','id');
$campaign_options = array('' => 'Choose One') + DB::table('campaigns')->lists('name','id');
$query = "SELECT a.id, a.form_id, a.metrics_id, a.response, b.response as responseoption, b.metrics_name, b.description, b.question, a.remarks FROM qcv.forms_responses a INNER JOIN metrics b ON a.metrics_id = b.id WHERE form_id = $id;";
$metric = DB::connection('mysql')->select($query);
return view('form.edit')->with(array('form'=> $form, 'agent_options' => $agent_options, 'campaign_options' => $campaign_options, 'metric' => $metric));
}
在我看来
<div class="col-md-6">
{!! Form::select('agent_id', $agent_options, '',array('class' => 'form-control', 'id' => 'agent_id', 'required' => 'required')) !!}
</div>
让我们使用我的 $agent_options
下拉菜单,它将在我的代理 table 中拉取代理列表,但在我看来,如何根据代理 ID 设置默认值 selected ?假设
agend_id = 1
默认为 select。
您需要将默认值传递给 select()
as
{!! Form::select('agent_id', $agent_options, $defaultValue, array('class' => 'form-control', 'id' => 'agent_id', 'required' => 'required')) !!}}
将 $defaultValue
替换为您需要的默认值 select。
参考DOC