【问题标题】:Set default value of dropdown from a Query Builder result list Laravel 5.1从查询生成器结果列表 Laravel 5.1 中设置下拉列表的默认值
【发布时间】:2015-09-29 08:49:57
【问题描述】:

我正在制作我的表单的编辑页面,并且我有一个选择下拉菜单,但我需要为该表单设置选定的值,因为它是一个编辑表单。我是这样做的:

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 下拉列表,它将在我的代理表中拉出代理列表,但在我看来,如何设置基于代理 ID 选择的默认值?比方说

agend_id = 1

默认选中。

【问题讨论】:

    标签: php laravel drop-down-menu query-builder


    【解决方案1】:

    您需要将默认值传递给select() as

    {!! Form::select('agent_id', $agent_options, $defaultValue, array('class' => 'form-control', 'id' => 'agent_id', 'required' => 'required')) !!}}
    

    $defaultValue替换为您需要默认选择的值。

    参考DOC

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2020-07-06
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      相关资源
      最近更新 更多