lundi 15 février 2016

Laravel 5 - updating child table

I have a database design like so Design

A contact_report can have many contact_report_actions. My tables may look something like this

Project

id    | data         | 
----------------------
1     | something    | 
----------------------

document

id    | data         | project_id
---------------------------------
1     | something    | 1
---------------------------------

document_actions

action         | deliveryDate  | documentID
--------------------------------------------
Walk dog       | 01/01/2016    | 1
--------------------------------------------
Wash clothes   | 08/02/2016    | 1
--------------------------------------------
Party          | 09/02/2016    | 1
--------------------------------------------

I am having a few issues updating the contact_report_actions table. At the moment I am trying something like this

public function update(Project $project)
{
    $project->contactReportDoc->discussionPoints = Input::get('discussionPoints');

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

    foreach($actions as $action => $input) {
        $contactReportActionsDoc = ContactReportActionsDoc::firstOrNew(['action' => $input["action"]]);
        $contactReportActionsDoc->action = $input["action"];
        $contactReportActionsDoc->responsibility = $input["responsibility"];
        $contactReportActionsDoc->save();
    }

    $project->contactReportDoc->update();
    $projectId = $project->id;

    $contactReportDoc = ContactReportDoc::where('projectId', '=', $projectId)->first();

    return Redirect::route('projects.contactReportDoc.edit', compact('project', 'contactReportDoc'));
}

$actions are the input of the user. What I have to do is see if the action already exists and if so, update it. Otherwise I need to create a new row for a new action. I cant seem to pass it the actions array and call the update on this because it complains that function update does not exist. How can I update rows within my contact_report_actions table?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire