Ad

Laravel 5 - Update Overwriting All Data

I have a Document Model which represents a Document (name, description etc). I then have a DocumentData Model which represents the data of a Document. A Document has One to Many DocumentData,

So to create a Document I have a form. Lets say that my form has a text input for Document Owner. Within my DocumentData table, the label documentOwner is set as the key and the inputted data is set as the value. So with multiple form elements, my DocumentData table might look like this

document_data

 id | documentId | key         |  value      |  
----------------------------------------------
 1  | 1          | clientName  |  Google     |   
----------------------------------------------
 2  | 1          | projectName |  Analytics  |  
----------------------------------------------
 3  | 1          | Contact     |  Mr Sharp   |  
----------------------------------------------
 4  | 1          | startDate   |  29/12/2016 |  
----------------------------------------------

The Document name and description for the Document table is created using hidden fields within the view of the Document.

So I can create Documents without a problem. I am having a problem with my update function though. So far I have the following

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") {
                if($key == $orgData->key) {
                    $orgData->value = $value;
                    $orgData->update();
                }
            }
        }
    }

    return View::make($document->name.'Doc.edit', compact('project', 'document'));
}

So firstly I get the Document I am working on and store it in $document. I then get the DocumentData for this Document and store it in $docData. $docData is a collection containing my key-value pairings for a Document.

I then loop both the $docData and the inputted data and where the keys match, I reset its updated value.

However, at the moment, it updates everything to the last inputted data field. I am not sure where else I can perform the update operation, but I only need it to update the row it is referring too, not the whole thing.

How could I go about doing this?

Thanks

Ad

Answer

I've cleaned up the code a little, and made a few implementation changes. This code should work, so let us know if it doesn't, and if not, what failed.

public function update(Request $request, Project $project, $id)
{
    // you can use eager loading and find on the relationship query
    $document = $project->document()->with('documentData')->find($id);
    // you can just access the relationship attribute, no need for the query
    $docData = $document->documentData;

    // gets all input, except the keys specified
    $input = $request->except(['filePath', 'documentType', 'documentTypeDesc']);

    foreach($docData as $orgData) {
        // check if the input has a key that matches the docdata key
        if (array_key_exists($orgData->key, $input)) {
            // update the value
            $orgData->value = $input[$orgData->key];
            // use save, not update
            $orgData->save();
        }
    }

    return View::make($document->name.'Doc.edit', compact('project', 'document'));
}
Ad
source: stackoverflow.com
Ad