How to make an update page with hasMany relatioins in laravel

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

+4
source share
1 answer

The icon +in PHP is not used to concatenate strings. This is a mathematical operator. To concatenate strings, you must use dot ( .).

So, change your code as follows:

Form::text('a' . $key, $value->arg1)
Form::text('b' . $key, $value->arg2)

a $key, .

, , , :

Form::text('items[' . $key . '][arg1]', $value->arg1)
Form::text('items[' . $key . '][arg2]', $value->arg2)

:

items => [
    [ arg1, arg2 ],
    [ arg1, arg2 ],
    ....
]
+1

All Articles