简体   繁体   中英

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'. I know that forms can be filled using 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:

Form::text("address");

And Laravel should supply the value from the model for you.

Please note, this assumes that you have a model for $purchaser defined. If not, do that first.

Usually, I just do <input> elements manually as I feel I have more control. 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.

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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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