Laravel 5 - update overwriting all data

落花浮王杯 提交于 2019-12-25 11:54:09

问题


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:


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


来源:https://stackoverflow.com/questions/34679268/laravel-5-update-overwriting-all-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!