1

I have a form which takes the following structure

<tr id='actionRow'>
    <td>
        <input type="text" name='actionInput[0][]' id="actionInput"  placeholder='Action' class="form-control"/>
    </td>
    <td>
        <select class="responsibility" name="actionInput[1][]" id="responsibilityInput">
            <option value=""></option>
            @foreach($users as $user)
                <option value="{{ $user->userName }}">{{ $user->userName }}</option>
            @endforeach
        </select>
    </td>
    <td>
        <input type="text" name='actionInput[2][]' id="dateInput" placeholder='Completion Date' class="form-control dateControl"/>
    </td>
</tr>

I have an option for the user to add rows to this form, and it basically clones the table row. The end result I get in my controller when I output actionInput is something like the following

array:3 [▼
  0 => array:3 [▼
    0 => "Action Input 1"
    1 => "Action Input 2"
    2 => "Action Input 3"
  ]
  1 => array:3 [▼
    0 => "Responsibility Input 1"
    1 => "Responsibility Input 2"
    2 => "Responsibility Input 3"
  ]
  2 => array:3 [▼
    0 => "Date Input 1"
    1 => "Date Input 2"
    2 => "Date Input 3"
  ]
]

When this data is added to my database table, a table row should consist of one bit of data from each array. So if I take the above data and insert it into a database, my table rows should look like the following

Action  |  Responsibility   | Date
-------------------------------------
Input 1 |  Responsibility 1 | Date 1
-------------------------------------
Input 2 |  Responsibility 2 | Date 2
-------------------------------------
Input 3 |  Responsibility 3 | Date 3
-------------------------------------

In my controller I am currently doing this

$actions = Input::get('actionInput');

foreach($actions as $action => $category) {
    $contactReportActions = new ContactReportActionsDoc();
    $contactReportActions->contactReportId = $contactReport->id;
    foreach($category as $key => $value){
        $contactReportActions->action = $value;
        $contactReportActions->responsibility = $value;
        $contactReportActions->deliveryDate = $value;
    }
    $contactReportActions->save();
}

The problem is, that produces something like the following

Action           |  Responsibility   | Date
----------------------------------------------------------
Input 1          |  Input 2          | Input 3
----------------------------------------------------------
Responsibility 1 |  Responsibility 2 | Responsibility 3
----------------------------------------------------------
Date 1           |  Date 2           | Date 3
----------------------------------------------------------

So how could I correct this within my controller so it inputs the data to my database correctly?

Thanks

1 Answer 1

1

I would do something like this:

<tr id='actionRow'>
    <td>
        <input type="text" name="actionInput[{{$key}}]['action']" id="actionInput"  placeholder='Action' class="form-control"/>
    </td>
    <td>
        <select class="responsibility" name="actionInput[{{$key}}]['responsibility']" id="responsibilityInput">
            <option value=""></option>
            @foreach($users as $user)
                <option value="{{ $user->userName }}">{{ $user->userName }}</option>
            @endforeach
        </select>
    </td>
    <td>
        <input type="text" name="actionInput[{{$key}}]['date_input']" id="dateInput" placeholder='Completion Date' class="form-control dateControl"/>
    </td>
</tr>

In the view, you should generate a random or sequential key to the array, and store the table's columns with their names. For each row you clone later, you generate another key.

This layout will also allow a easier entity edit if you need to.

Then, in the Controller, you can separate each entity property like this:

    foreach (\Input::get('actionInput', array()) as $entityData)
    {
        $entity = ContactReportAction::find($entityData["id"]) ?: new ContactReportAction;
        $entity->fill($entityData);
        $entity->save();
    }

Check if this helps you...

Sign up to request clarification or add additional context in comments.

6 Comments

Just a quick question - How could I generate the random key, and what is $class?
The $key is just a random or sequential key, what are you doing to clone the rows? Are you using javascript?
Yes, I am using javascript to clone.
You are already counting in your javascript in i variable. So, you can use it to create an attribute like key, and then use it to change the names of the inputs in the row you cloned with something like $name = $name.replace('['+($key-1)+']', '['+$key+']');
In the controller, what is $entityData["id"]? It currently has no id so it complains about this variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.