简体   繁体   English

Laravel形式模型绑定

[英]Laravel form model binding

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? 我可以使用表单模型绑定来执行此操作,还是必须手动处理表单?

Or, failing that, can I use form model binding for the user fields, but manually handle the address fields? 或者,如果不这样做,我可以对用户字段使用表单模型绑定,但是手动处理地址字段吗?

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() ) ,你会得到这样的东西:

这是Input :: all()结果 This result is provided by Kint's dd(): https://github.com/raveren/kint . 这个结果由Kint的dd()提供: https//github.com/raveren/kint Really helpful. 真有帮助。

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

        ...
    }

    ...
}

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() !!}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM