Laravel形式模型绑定 [英] Laravel form model binding

查看:116
本文介绍了Laravel形式模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读此功能: http://laravel.com/docs/html#form-模型绑定

I've been reading about this feature: 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?

否则,我可以为用户使用表单模型绑定字段,但手动处理地址字段?

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

推荐答案

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

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

您传递的模型($ user)

The model ($user) you pass in

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();

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

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

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

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.

<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>

如果你执行 dd(Input :: all() code>在你的控制器,你会得到这样的:

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


此结果由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);

        ...
    }

    ...
}

这篇关于Laravel形式模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆