Php Laravel-4如何使用id值和名称值从数据库填充选择框

Php Laravel-4如何使用id值和名称值从数据库填充选择框,php,select,laravel,laravel-4,Php,Select,Laravel,Laravel 4,我想用项目控制器中我的数据库中的客户端填充一个选择框,因为项目将属于客户端,但它也属于登录的用户 我想创建一个如下所示的选择框: <select> <option value="$client->client_id">$client->client_name</option> <option value="$client->client_id">$client->client_name</option>

我想用项目控制器中我的数据库中的
客户端
填充一个选择框,因为项目将属于
客户端
,但它也属于登录的用户

我想创建一个如下所示的选择框:

<select>
  <option value="$client->client_id">$client->client_name</option>
  <option value="$client->client_id">$client->client_name</option>
</select>
create.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>
// 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')) }}
{{Form::open(数组('action'=>)ProjectController@store','id'=>'createproject'))}
@如果(计数($client_selector)>0)
{{Form::label('select_client','select client',array('class'=>'awesome'));}
{{Form::select('client',$client\u selector,数组值($client\u selector)[0])}
@恩迪夫
{{Form::label('project_name','project name')}
{{Form::text('project_name',Input::old('project_name'),array('class'=>'Form control'))}
从创建select的方式可以看出,它使用client_名称填充values属性,我在Laravel方面不是很专业,所以我不确定如何更改这些属性

如果有人可以告诉我这是如何做到的,或者有更好的方法来实现这一点,那么请给我一些例子

提前感谢。

找到了这样做的方法

ClientController.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));
}
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

{{ 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>
// 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')) }}

希望这能帮助其他有类似问题的人。

我也考虑过这一点,并提出了以下建议:

在我的模型中:

public function scopeSelect($query, $title = 'Select') {
    $selectVals[''] = $title;
    $selectVals += $this->lists('name', 'id');
    return $selectVals;
}
那么我可以把这一点放在我的观点中:

{{ Form::select('select_id', Model::Select('Blank option'), '', array()) }}

在这里,您可以使用列表方法。在您的客户机控制器中

$clients = Client::lists('name', 'id');
因此,只需使用如下变量

{{Form::select('client', $clients)}}

直接按名称调用表不是一种糟糕的做法吗?这样会得到“不受支持的操作数类型”,可能是这样的?公共函数scopeForSelectBox($query,$title='Country'){$countries=$query->lists('name','id')->toArray();array_unshift($countries,$title);return$countries;}Laravel 5.3中,删除了lists()方法而改用pull()方法。例如:$clients=Client::pull('name','id');