5

阅读文档后确定:http: //four.laravel.com/docs/html#form-model-binding

我有一个看起来像这样的表格:

{{ Form::model($profile, array('action' => 'ProfilesController@edit', $profile->user_id, 'files' => true)) }}
{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), array('class' => 'span12')) }}
{{ From::close() }}

我的问题是:模型绑定不适用于 Form::select,在文本输入中效果很好。我究竟做错了什么??

谢谢你的帮助。

4

2 回答 2

10

我认为您在选择中的第三个参数需要是选定的值:

{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), $profile->gender) }}

我知道这有点违背模型绑定的目的,但它实际上会起作用。另一个问题当然是现在你已经失去了你的课程!

但是,如果我们快速浏览一下 api:

select( string $name, array $list = array(), string $selected = null, array $options = array() )

我们看到您可以将您的选项数组作为第四个参数传递。

因此,工作代码是:

{{ Form::select('gender', array('0' => '你是什么性别?', '1' => '男', '2' => '女'), $profile->gender , 数组('class' => 'span12')) }}

于 2013-07-15T17:20:48.070 回答
6

kJamesy 是对的,第三个参数必须是选择的值,但是如果设置为null,表单模型绑定会设置默认值。

{{ Form::model($profile, array('action' => 'ProfilesController@edit', $profile->user_id, 'files' => true)) }}
{{ Form::select('gender', array('0' => 'What gender are you?', '1' => 'Male', '2' => 'Female'), null, array('class' => 'span12')) }}
{{ From::close() }}
于 2014-02-11T02:46:13.977 回答