1
votes

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

1
Can you give us an example of the data being posted, as well as the data structure within $docData? It's difficult to interpret exactly how the data in each place should be being set. Currently, you're setting the same posted input data for every $orgData in the loop. This is most likely why your value is always the last one since with each iteration on the input data, you're overriding the $orgData->value previously set.jardis
Sure, no problem, I have updated the original post which better shows the problem.katie hudson

1 Answers

4
votes

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'));
}