vendredi 12 février 2016

Laravel 5 - More complex displaying of old data

I have a simple database design like so

DB Design

So a contact_report can have one to many contact_report_actions. In my create view, you can add rows to the table to add further actions, this uses clone to duplicate the input row. I pass the create view a list of users for the select options. The create view looks something like this

<tr id='actionRow1'>
    <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}}][deliveryDate]' id="dateInput" placeholder='Completion Date' class="form-control dateControl"/>
    </td>
</tr>

The end result is a database table like so

action       |  responsibility  | deliveryDate  | contact_report_id
--------------------------------------------------------------------
Walk dog     |  John Doe        | 01/01/2016    | 1
--------------------------------------------------------------------
Wash clothes |  Jane Doe        | 08/02/2016    | 1
--------------------------------------------------------------------
Party        |  Kelly Doe       | 09/02/2016    | 1
--------------------------------------------------------------------

In my edit controller action, I do something like this

public function edit(Project $project)
{
    $actionPoints = ContactReportActionsDoc::where('contactReportId', '=', $project->contactReportDoc->id)->get();

    return View::make('contactReportDoc.edit', compact('project', 'actionPoints'));
}

So in my edit view, I do a foreach loop for these action rows and if I output $actions I get the following

#attributes: array:7 [▼
    "id" => 10
    "action" => "Walk dog"
    "responsibility" => "John Doe"
    "deliveryDate" => "01-01-2016"
    "contactReportId" => 1
    "created_at" => "2016-02-12 10:26:08"
    "updated_at" => "2016-02-12 10:26:08"
]

So my data is all there. In my edit view, I am currently doing something like so

@foreach($actionPoints as $key => $action)
    <tr id='actionRow{{$key}}'>
        <td>
            <input type="text" name='actionInput[{{$key}}][action]' id="actionInput"  value={{$action->action}} class="form-control"/>
        </td>
        <td>
            {{--{!! Form::select('actionInput[1][]', Input::old($action->responsibility), ['class' => 'responsibility', 'id' => 'responsibilityInput']) !!}--}}
        </td>
        <td>
            <input type="text" name='actionInput[{{$key}}][deliveryDate]' id="dateInput" value={{ $action->deliveryDate }} class="form-control dateControl"/>
        </td>
    </tr>
@endforeach

The first problem is with the select which I have commented out. I am passing it an array which contains the selected option.

$action->responsibility

However, I really need to display the list of users like before, with the selected option as the current selected one.

How could I go about doing something like this?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire