0

我已经设置了我的项目与客户相关的关系,我想要做的是能够通过下拉列表在我的创建项目视图中选择一个选项。

这可以这样实现:

{{ Form::select('client', array('s' => 's', 'm' => 'm'), 's');}}

但是我不确定如何从我从数据库中检索的数据动态地执行此操作,我到目前为止的代码是:

{{ Form::open(array('action' => 'ProjectController@store', 'id' => 'createproject')) }}
  <div class="form-group">
  <? $projects = Auth::user()->clients; ?>
@if (Auth::check())
    @if (count($projects) > 0)
   @foreach ($projects as $client)

{{ Form::select('client', array('client_one' => 'client_one', 'client_two' => 'client_two'), 'client_one');}}

   @endforeach 
   @else
    <h3>You have no projects click <a href="/project/create">here to create a project</a></h3>
@endif
@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>

谁能指出我如何动态填充此选择字段的方向?

提前致谢!

4

2 回答 2

3

在视图中进行数据处理不是一个好主意。您应该做的是在控制器中准备数组并在视图中使用该数组。

在你的控制器中。

    $clients = Auth::user()->clients;
$client_selector = array();

foreach($clients as $client) {
    $client_selector[$client->name] = $client->name; // I assume name attribute contains client name here
}

return View::make('your.view', array(.., 'client_selector' => $client_selector, ...));

在你看来。

@if(count($client_selector)>0)
    {{Form::select('client', $client_selector, array_values($client_selector)[0])}}
@endif 
于 2013-11-11T19:20:32.957 回答
0

我认为这是一个更好的解决方案:

在您的控制器中:

$clients = Auth::user()->clients->lists('name', 'name');

return View::make('your.view', array(.., 'clients' => $clients, ...));

在您看来:

    {{ Form::select('client', $clients) }}
于 2014-07-10T13:34:21.650 回答