Laravel集体形式和foreach循环

Laravel collective form and foreach loop

我想将我的下拉 select 表单连接到数据库,目前我有这样的东西:

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

这是我的控制器:

        $clients = Client::all();

        return view('report_create')->with('clients', $clients);

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

如果您想创建 select 个客户列表,请使用 pluck():

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

要使其正常工作,您还需要 define an accessorClient 模型中:

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

然后创建列表:

{!! Form::select('connected_with', $clients) !!}