Associating a Laravel Form Form with Relationships

Is it possible to associate a form with a model that has a relationship? For example, I have an order model that has one to many with a detailed model. It will save a lot of time with

@foreach($order->details as $detail)
{{ Form::text('product_name', Input::old('product_name') ? Input::old('product_name') : detail->product_name)
@endforeach
+4
source share
2 answers

For a relationship, one-to-oneyou can use something like this:

Form::text('detail[product_name]')

In this case, it $order->detail->product_namewill be filled in this text box if the instance is Orderattached to fromusing Form::model($order)with the appropriate model Detail, but it may not be possible one-to-many, because there will simply be a collection, and you need a loop.

+6
source

To fulfill @WereWolf's answer ..

  • product_name detail_names
  • , : orders.1.product_name
  • Input::old() Input::get() , DB .

.

Form::text('detail_names['.$detail->id.']', Input::old('detail_names.'.$detail->id, $detail->product_name))

- :

foreach(Input:get('detail_names') as $id => $product_name)
{
    //...
}

, .

+2

Source: https://habr.com/ru/post/1539122/


All Articles