php Laravel 表单模型绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16873638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:49:55  来源:igfitidea点击:

Laravel form model binding

phpformslaravellaravel-4eloquent

提问by eimajenthat

I've been reading about this feature: http://laravel.com/docs/html#form-model-binding

我一直在阅读有关此功能的信息:http: //laravel.com/docs/html#form-model-binding

And it looks really neat, but there are couple of things that I'm not certain about.

它看起来非常整洁,但有几件事我不确定。

Do I need to put any code in the controller action to process this form? What does that look like?

我是否需要在控制器操作中添加任何代码来处理此表单?那看起来像什么?

The model (User) I want to bind in my form has a separate table for addresses. So I want to be able to fill out the User model's fields, but also the fields for the related Address model. Can I do that with form-model-binding, or do I have to handle the form manually?

我想在表单中绑定的模型(用户)有一个单独的地址表。所以我希望能够填写 User 模型的字段,以及相关 Address 模型的字段。我可以使用表单模型绑定来做到这一点,还是必须手动处理表单?

Or, failing that, can I use form model binding for the user fields, but manually handle the address fields?

或者,如果失败,我可以对用户字段使用表单模型绑定,但手动处理地址字段吗?

回答by Antonio Carlos Ribeiro

You don't need any different code in your controller to process this form. All your (named) form variables will be in Input::all().

您不需要在控制器中使用任何不同的代码来处理此表单。所有(命名)表单变量都将在 Input::all() 中。

The model ($user) you pass in

您传入的模型($user)

Form::model($user, array('route' => array('user.update', $user->id)))

Is just any record you need to, if you have more than one table involved, you'll have to do something like

只是您需要的任何记录,如果涉及多个表,则必须执行以下操作

$user = User::where('id',$userID)
           ->leftJoin('users_addresses', 'users_addresses.user_id', '=', 'users.id')
           ->first();

And pass this composed model to your Form::model().

并将这个组合模型传递给您的 Form::model()。

How you name your inputs is entirely up to you, because you'll have to write the logic to process your form. But, in my opinion users_address[street]for the address inputs is good, because you'll end up with an array of addresses columns that you can pass right away to your UserAddress model.

您如何命名输入完全取决于您,因为您必须编写逻辑来处理表单。但是,在我看来users_address[street],地址输入很好,因为您最终会得到一个地址列数组,您可以立即将这些列传递给您的 UserAddress 模型。

<html>
    <head>
        <title></title>
    </head>
    <body>
        {{ Form::model($user, array('route' => array('user.update', $user->id))) }}
            {{ Form::label('first_name', 'First Name:', array('class' => 'address')) }}
            {{ Form::text('first_name') }}

            {{ Form::label('last_name', 'Last Name:', array('class' => 'address')) }}
            {{ Form::text('last_name') }}

            {{ Form::label('email', 'E-Mail Address', array('class' => 'address')) }}
            {{ Form::text('email') }}

            {{ Form::label('address[street1]', 'Address (Street 1)', array('class' => 'address')) }}
            {{ Form::text('address[street1]') }}

            {{ Form::label('address[street2]', 'Address (Street 2)', array('class' => 'address')) }}
            {{ Form::text('address[street2]') }}

            {{ Form::label('ddress[city]', 'City', array('class' => 'address')) }}
            {{ Form::text('address[city]') }}

            {{ Form::label('address[state]', 'State', array('class' => 'address')) }}
            {{ Form::text('address[state]') }}

            {{ Form::label('address[zip]', 'Zip Code', array('class' => 'address')) }}
            {{ Form::text('address[zip]') }}

            {{ Form::submit('Send this form!') }}
        {{ Form::close() }}
    </body>
</html>

And if you do dd( Input::all() )in your controller, you'll get something like this:

如果你dd( Input::all() )在你的控制器中这样做,你会得到这样的东西:

This is the Input::all() resultThis result is provided by Kint's dd(): https://github.com/raveren/kint. Really helpful.

这是 Input::all() 结果此结果由 Kint 的 dd() 提供:https: //github.com/raveren/kint。真的很有帮助。

If your form just have fields from a single Model, your update method can be very simple and look something like:

如果您的表单只有来自单个模型的字段,则您的更新方法可能非常简单,如下所示:

public function update($id)
{
    $user = User::find($id);

    if (!$user->update(Input::all())) {
        return Redirect::back()
                ->with('message', 'Something wrong happened while saving your model')
                ->withInput();
    }

    return Redirect::route('user.saved')
                ->with('message', 'User updated.');
}

On forms a little bit more complex, coders will have to add more logic to their controllers, in you case with a little bit more of research I think you can make this happen:

在稍微复杂一点的表单上,编码人员必须向他们的控制器添加更多逻辑,在你的情况下,通过更多的研究,我认为你可以做到这一点:

public function update($id)
{
    $user = User::find($id);

    $inputs = Input::all();

    if (!$user->update($inputs)) {
            $address = new UserAddress($inputs['address']);

        $user->address()->save($address);

        ...
    }

    ...
}

回答by Dukadin

In Laravel 5.1 for relation model binding you just need to eager load relation table(s):

在 Laravel 5.1 的关系模型绑定中,您只需要急切加载关系表:

$user = User::with(['address'])->find($id);

And in view set fields names as array:

并在视图中将字段名称设置为数组:

{!! Form::model($user, ['route' => ['user.update', $user->id]]) !!}
    {!! Form::text('address[street]') !!}
    {!! Form::text('address[number]') !!}
{!! Form::close() !!}