在表单中嵌套模型关系 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20684932/
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-09-14 08:46:48  来源:igfitidea点击:

Nesting Models Relations In Forms - Laravel

phpruby-on-railslaravellaravel-4

提问by Goldentoa11

In laravel, is there some way of nesting related resources in a form?

在laravel中,是否有某种方式将相关资源嵌套在表单中?

Say I have this:

说我有这个:

class Person extends Eloquent {
  public function addresses() {
    return $this->hasMany("Address");
  }
}

class Address extends Eloquent {
  public function person() {
    return $this->belongsTo("Person");
  }
}

and I want a Personform to collect information about that Person's Addresses. Does laravel facilitate this in a way that is equivalent to Rails' accepts_nested_attributes_for :addressand fields_for :address?

我想要一个Person表格来收集关于那个Person的信息Address。laravel 是否以与 Railsaccepts_nested_attributes_for :address和等效的方式促进了这一点fields_for :address

I'd just like something simple where I can include the Addressfields with the results of the Personform, since the Addressdoesn't really exist apart from the Person. Does this make sense?

我只是想要一些简单的东西,我可以在其中包含表单Address结果的字段Person,因为Address除了Person. 这有意义吗?

== EDIT ==

== 编辑 ==

This is hypothetical code

这是假设的代码

What I'm looking for is something that would resemble this:

我正在寻找的是类似于这样的东西:

{{ Form::model(new Person, array("action" => "admin\PersonController@store", "method" => "POST")) }}

{{ Form::text("name", array(...)) // <input name='person[name]' ... /> }}


{{ Form::email("email", array(...)) // <input name='person[email]' ... /> }}

{{ Form::fields_for("addresses"/* Would be name of relation */) }}

  {{ Form::text("street_address") // <input name='person[addresses][][street_address]' ... /> }}

{{ Form::close_fields() }}

{{ Form::close() }}

回答by Collin James

You are on the right track with the input names.

您在输入名称的正确轨道上。

Form

形式

// Form open, Person fields, etc...

<h2>Addresses</h2>
@foreach ($addresses as $address)

    <fieldset>

        {{ Input::text('addresses['.$address->id.'][address_1]', $address->address_1) }}
        {{ Input::text('addresses['.$address->id.'][address_1]', $address->address_2) }}
        {{ Input::text('addresses['.$address->id.'][city]', $address->city) }}
        {{ Input::text('addresses['.$address->id.'][state]', $address->state) }}
        {{ Input::text('addresses['.$address->id.'][zip]', $address->zip) }}

    </fieldset>

@endforeach

// Form Close

If you want to add addresses you'll need to generate some random key to use instead of the address id. This will keep the fields grouped.

如果你想添加地址,你需要生成一些随机密钥来代替地址 ID。这将使字段保持分组。

Controller Logic

控制器逻辑

This is how I would handle input, using 'fillable' to filter the data going into the models.

这就是我处理输入的方式,使用“可填充”来过滤进入模型的数据。

// Get the Person model, fill, save, etc...

$addressIds = array();
foreach (Input::get('addresses', array()) as $id => $addressData)
{
    $address = Address::find($id) ?: new Address;
    $address->fill($addressData);
    $address->save();
    $addressIds[] = $address->id;
}

$changes = $person->addresses()->sync($addressIds);

// Delete the unused addresses
foreach ($changes['detached'] as $detachedAddressId)
{
    $address = Address::find($detachedAddressId);
    if (!empty($address)) $address->delete();
}

回答by Anderson Danilo

Now you can use the package "modelform" to create a form for various models or even a set of forms for relations.

现在您可以使用包“modelform”为各种模型创建一个表单,甚至为关系创建一组表单。

https://github.com/andersondanilo/modelform

https://github.com/andersondanilo/modelform

回答by clod986

You should manage all of this from your controller. Collect the data from the form and use them as you please:

您应该从控制器管理所有这些。从表格中收集数据并根据需要使用它们:

// Controller Code
$person = new Person(array(
    'field_1' => Input::get('field_1'),
    'field_2' => Input::get('field_2'),
    .............
));
$person->save();

$address = new Address(array(
    'field_x' => Input::get('field_x'),
    'field_y' => Input::get('field_y'),
    .............
));
$person->addresses()->save($address);

See it in the docs

在文档中查看