php Laravel-4 如何使用 id 值和名称值从数据库中填充选择框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20019369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel-4 how to populate select box from database with id value and name value
提问by 001221
I want to populate a select box with clientsfrom my database in the projects controller as a project will belong to a clientbut it also belongs to the user who is logged in.
我想clients从我的项目控制器中的数据库中填充一个选择框,因为项目将属于 aclient但它也属于登录的用户。
I want to create a select box like the one below:
我想创建一个如下所示的选择框:
<select>
<option value="$client->client_id">$client->client_name</option>
<option value="$client->client_id">$client->client_name</option>
</select>
I have this in laravel which populates my select field with the client names however the valueattribute has the client name and I would rather this had the client_id.
我在 Laravel 中有这个,它用客户端名称填充我的选择字段,但是该value属性具有客户端名称,我宁愿它具有 client_id。
The way I have done this is as below:
我这样做的方法如下:
ProjectController.php
项目控制器.php
public function create()
{
//find logged in users clients
$clients = Auth::user()->clients;
$client_selector = array();
foreach($clients as $client) {
$client_selector[$client->client_name] = $client->client_name;
}
// load the create form (app/views/projects/create.blade.php)
return View::make('projects.create', array('client_selector' => $client_selector));
}
create.blade.php
创建.blade.php
{{ Form::open(array('action' => 'ProjectController@store', 'id' => 'createproject')) }}
<div class="form-group">
@if(count($client_selector)>0)
{{ Form::label('select_client', 'Select Client', array('class' => 'awesome')); }}
<!-- SELECT IS CREATED HERE -->
{{Form::select('client', $client_selector, array_values($client_selector)[0])}}
@endif
</div>
<div class="form-group">
{{ Form::label('project_name', 'Project Name') }}
{{ Form::text('project_name', Input::old('project_name'), array('class' => 'form-control')) }}
</div>
As you can see from the way the select is being created it is using the client_name to populate the values attributes and I'm not really much of an expert on Laravel so I'm not sure how to change these attributes.
从创建选择的方式可以看出,它使用 client_name 来填充值属性,而我并不是 Laravel 的专家,所以我不确定如何更改这些属性。
If any could perhaps show me how this is done or has a better method of achieving this then please do give me some examples!
如果有人可以告诉我这是如何完成的,或者有更好的方法来实现这一点,那么请给我一些例子!
Thanks in advance.
提前致谢。
回答by 001221
Found a way to do this
找到了一种方法来做到这一点
ClientController.php
客户端控制器.php
public function create() {
// queries the clients db table, orders by client_name and lists client_name and id
$client_optons = DB::table('clients')->orderBy('client_name', 'asc')->lists('client_name','id');
return View::make('projects.create', array('client_options' => $client_options));
}
create.blade.php
创建.blade.php
// this form is now populated with the value as id and the option names as the client_name
{{ Form::select('clients', $client_options , Input::old('clients')) }}
Hope this helps others having problems like this.
希望这可以帮助其他有类似问题的人。
回答by Wally
I also had a think about this and came up with the following:
我也考虑过这个问题,并提出以下几点:
In my model:
在我的模型中:
public function scopeSelect($query, $title = 'Select') {
$selectVals[''] = $title;
$selectVals += $this->lists('name', 'id');
return $selectVals;
}
Then I can just put this in my views:
然后我可以把它放在我的观点中:
{{ Form::select('select_id', Model::Select('Blank option'), '', array()) }}
回答by Jyothu
Here you can use listsmethod. In your client controller do
在这里您可以使用列表方法。在您的客户端控制器中执行
$clients = Client::lists('name', 'id');
And in view, just use the variable like,
在视图中,只需使用变量,例如,
{{Form::select('client', $clients)}}

