I am trying to create a form for updating a model that has a one-to-many relationship. The structure is similar to
item => {[arg1, arg2], [arg1, arg2], [arg1, arg2]}
Each related model has two fields for updating. I used the code below, but I do not know how to encode the controller.
@extends('app')
@section('content')
{!! Form::open(array('route' => array('Topic.update', $topic->id), 'method' => 'PATCH')) !!}
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', $topic->title, array('maxlength' => 250)) !!}
@foreach($topic->pairs as $key=>$value)
<div>
{!! Form::label('title', 'Pair ' . $key . ': ') !!}
{!! Form::text('a' + $key, $value->arg1) !!}
{!! Form::text('b' + $key, $value->arg2) !!}
</div>
@endforeach
{!! Form::submit() !!}
{!! Form::close() !!}
@endsection
When I dd ($ response-> all ()), it displays below:
array:9 [▼
"_method" => "PATCH"
"_token" => "dInpiSa6O2KIMftFnICQtVM873nUF2Zp2HlzeN4S"
"title" => "Actors and Movies"
0 => "Rain Man"
1 => "Rush Hour"
2 => "Edward Scissorhands"
3 => "Titanic"
4 => "The Devil Wears Prada"
5 => "Fast & Furious"
]
Related items are not pairs, only arg1. So, how should I code the hasMany model? Thanks
source
share