Laravel集体形式和foreach循环
问题描述:
I want to connect my dropdown select form with database, currentli I have sth like this:
@foreach( $clients as $client)
{!! Form::select('connected_with',
['name' => $client->name . $client->surname
]) !!}
@endforeach
And this is my controller:
$clients = Client::all();
return view('report_create')->with('clients', $clients);
and i get much fields. I want only one with items from db. How to do it?
我想将我的下拉选择表单与数据库连接,currentli我有这样的: p>
这是我的控制器: p>
我得到了很多字段。 我只想要一个来自db的项目。 怎么做? p>
div> @foreach($ clients as $ client)
{!! Form :: select('connected_with',
['name'=> $ client-> name。$ client-> surname
])!!}
@endforeach
code> pre>
$ clients = Client :: all();
返回视图('report_create') - > with('clients',$ clients);
code> pre>
答
If you want to create select list of clients, use pluck()
:
$clients = Client::pluck('full_name', 'id');
return view('report_create')->with('clients', $clients);
To make it work, you'll also need to define an accessor in the Client
model:
public function getFullNameAttribute()
{
return $this->name.' '.$this->surname;
}
Then just create the list:
{!! Form::select('connected_with', $clients) !!}