Laravel使用数据库保存字段填写表单

时间:2014-01-31 06:56:27

标签: php laravel laravel-4

以下代码是我的用户个人资料管理表单,我想用input变量填充$profile代码。

{{ $profile }}可以回显用户表的所有字段。但是我无法使用此变量填充input标签。

结果:{"id":"1","username":"mahdi","name":"Mahdi","family":"Pishguy","email":"name@gmail.com"}

我的表格:

{{ Form::open(array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
                <div class="form-group rtl">
                    <div>Your Username: <b> {{ $profile->username }} </b></div><br />
                    {{ Form::label('name' , 'name: ') }}
                    {{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}

                    {{ Form::label('family' , 'family: ') }} 
                    {{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}

                    <p>
                    {{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
                    </p>
                </div>
{{ Form::close() }}

1 个答案:

答案 0 :(得分:2)

很容易,请参阅Form Model Binding in the docs

您的代码应该是这样的:

{{ Form::model($profile, array('route' => 'admin.profile.update', $profile->id , 'method' => 'PUT','class'=>'navbar-form navbar-right', 'role' =>'search')) }}
                <div class="form-group rtl">
                    <div>Your Username: <b> {{ $profile->username }} </b></div><br />
                    {{ Form::label('name' , 'name: ') }}
                    {{ Form::text('name', Input::old('name'), array('id'=>'email', 'class' => 'form-control')) }}

                    {{ Form::label('family' , 'family: ') }} 
                    {{ Form::text('family', Input::old('family'), array('placeholder'=>'sss', 'id'=>'email', 'class' => 'form-control')) }}

                    <p>
                    {{ Form::submit('Submit', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}
                    </p>
                </div>
{{ Form::close() }}

注意:您可以移除Input::old() stuf,因为这是由Form::model()

处理的