Laravel集体形式和foreach循环

时间:2017-07-06 10:05:35

标签: php laravel laravelcollective

我想将我的下拉选择表单与数据库连接,currentli我就像这样:

<div class="input text"><label for="id">Id</label><input type="text" name="id" id="id"/></div>
<script>
    var testTags = ['52332', '56346', '5734'];
    var tags = '/invoices/search'
    jQuery('#id').autocomplete({
        source: testTags,
        minLength: 1
    });
</script>    </div>

这是我的控制者:

                 @foreach( $clients as $client)
                    {!! Form::select('connected_with',
                     ['name' => $client->name . $client->surname
                      ]) !!}
                    @endforeach

我得到了很多字段。我只想要一个来自db的项目。怎么做?

1 个答案:

答案 0 :(得分:0)

如果要创建客户的选择列表,请使用pluck()

$clients = Client::pluck('full_name', 'id');
return view('report_create')->with('clients', $clients);

要使其有效,您还需要Client模型中的define an accessor

public function getFullNameAttribute()
{
    return $this->name.' '.$this->surname;
}

然后只需创建列表:

{!! Form::select('connected_with', $clients) !!}
相关问题