简体   繁体   English

Laravel使用输入组填充表单

[英]Laravel populate form with input groups

I am looking for the way to bind data in to form with input groups. 我正在寻找将数据绑定到输入组中的方式。 For exmaple I have these inputs: 例如,我有这些输入:

Form::text('purchaser[address]');
Form::text('seller[address]');

and I want to bind data from my variable - $purchaser which has a field 'address' and $seller also with field 'address'. 我想绑定来自变量-$ purchaser的数据,该变量具有字段“ address”,而$ seller也具有字段“ address”。 I know that forms can be filled using laravel 我知道可以使用laravel填写表格

Form::model

but I can't find example how to use it for input groups. 但我找不到如何将其用于输入组的示例。 Is there even such a possibility or I have to do it manually? 甚至有这种可能性,还是我必须手动进行?

From the docs, I think you're pretty close: 从文档中,我认为您非常接近:

Form::model($purchaser);

Then, if you want an <input type="text"> with the address value in it, you would call: 然后,如果您想要一个带有地址值的<input type="text"> ,则可以调用:

Form::text("address");

And Laravel should supply the value from the model for you. Laravel应该为您提供模型的价值。

Please note, this assumes that you have a model for $purchaser defined. 请注意,这假设您已经为$purchaser定义了一个模型。 If not, do that first. 如果不是,请先执行此操作。

Usually, I just do <input> elements manually as I feel I have more control. 通常,我只是手动执行<input>元素,因为我感觉自己有更多的控制权。 In this case: 在这种情况下:

<input type="text" name="address" value="{{ $purchaser->address }}" class="..."/>

And I can edit and modify that whenever I want, especially the class and value attributes. 而且我可以随时进行编辑和修改,尤其是classvalue属性。

Hope that helps! 希望有帮助!

Edit: I got it wrong I suppose. 编辑:我猜错了。 You want just fill the address field value, not address relation, but with a group of models, right? 您只想填写address字段值,而不是address关系,而是填写一组模型,对吗?

So, you can easily do this: 因此,您可以轻松地做到这一点:

$purchaser = Purchaser::with('address')->find($someId);

$groups = new Illuminate\Support\Collection;

$groups->put('purchaser', $purchaser);
$groups->put('another_model', $anotherModel);

// then
Form::model($groups->toArray())
  Form::text('purchaser[address]')
  Form::text('another_model[some_field]')

First you need to load the address , then you need to call it in array manner: 首先,您需要load address ,然后需要以数组方式调用它:

// eager load
$purchaser = Purchaser::with('address')->find($someId);
// or lazy load
$purchaser->load('address');

// then:
Form::model($purchaser, ...)
  Form::text('address[street]')
  Form::text('address[city]')
  // and so on

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

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