vendredi 8 janvier 2016

Laravel 5 - update overwriting all data

I have a Document Model, which has many DocumentData. DocumentData essentially holds the data of a document.

My update function is

public function update(Request $request, Project $project, $id)
{
    $document = $project->document()->where('id', '=', $id)->first();
    $docData = $document->documentData()->get();

    $input = $request->all();

    foreach($docData as $orgData) {
        foreach($input as $key => $value) {
            if($key !== "filePath" && $key !== "documentType" && $key !== "documentTypeDesc") {
                $orgData->value = $value;
                $orgData->update();
            }
        }
    }

    return View::make('documentA.edit', compact('project', 'document'));
}

Firstly, I get the Document I am working on. I then get all the data for that Document.

Next, I get the input from the user, which represents the new data for this Document.

I then loop both the original data, and the new input, and set the original value to the new value. The last step is to update this data.

At the moment, every bit of data within a Document is being updated to the last input field value. I understand why this is happening, but I do not see anywhere else I update the DocumentData.

Is there any way to update this without setting everything to the last input?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire