Laravel:如何用会话中的值填充形式


Laravel: How to Populate Form with Values from Session

我得到了这个表单,它将值存储到会话中,因为它是多步骤过程的一部分。单击"下一步"时,这些值将存储在会话中,然后重定向到步骤2。

然而,在步骤2中,您可以选择返回步骤1进行更正。现在,我想用已经存储在会话中的值填充步骤1的表单,这样用户就不必重写所有内容。

以下是步骤1的形式:

    {!! Form::open(['route' => 'orders.store', 'data-toggle' => 'validator', 'class' => 'form-horizontal']) !!}
        <div class="form-group">
            {!! Form::label('household_count', 'What is the householdCount?', ['class' => 'col-sm-2']) !!}
            <div class="col-sm-10"> 
                <label class="radio-inline">
                {!! Form::radio('householdCount', '1', false, ['required' => 'required']) !!} 1
                </label>
                <label class="radio-inline">
                {!! Form::radio('householdCount', '2', false, ['required' => 'required']) !!} 2
                </label>
            </div>
        </div>
        <div class="form-group">
            {!! Form::label('city', 'Where do you live?', ['class' => 'col-sm-2']) !!}
            <div class="col-sm-10"> 
                {!! Form::text('city', null, ['class' => 'form-control', 'placeholder' => 'e.g. Berlin', 'required']) !!}
                <div class="help-block with-errors"></div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                {!! Form::submit('Next', ['class' => 'btn btn-primary btn-next']) !!}
            </div>
        </div>
    {!! Form::close() !!}

这是创建表单的方法。

public function step1() {
    return view('step1.create');
}

我已经找到了:http://laravel.com/docs/5.0/html#form-模型绑定

但每当我更换窗体::open()具有形式::model()

"我的表单"刚刚中断,表单也没有填充。

以下是存储在我的会话中的内容:

{"_token":"iThJYWxZj3APr7Unzrczd4WrGfs6dGsUtKINyTIC","_previous":{"url":"http:'/'/uploader.dev"},"flash":{"old":[],"new":[]},"householdCount":"2","city":"adklf","comment":"akldfjlakdjf","comments":"lkjaldfj"}

那么,如何使用存储在该会话中的值填充表单呢?

检索会话数据并将其填充到关联数组中。然后,将该数组传递给Form::model()标记。

// Populate your array in the controller.
$data = [
  'name' => 'Cryode',
  'age'  => 29,
];
return View::make('myForm', ['modelData' => $data]);
// Use Form::model() in your view.
{{ Form::model($modelData, [ ... ]) }}

Form::model()不需要Eloquent模型。它也可以使用标准对象或关联数组。