Laravel 5 - 更新覆盖所有数据 [英] Laravel 5 - update overwriting all data
问题描述
所以要创建一个文档我有一个表单。让我们说我的表单有一个文本输入文档所有者。在我的DocumentData表中,将documentOwner标签设置为键,并将输入的数据设置为该值。所以使用多个表单元素,我的DocumentData表可能看起来像这样
document_data
id | documentId |关键|值|
----------------------------------------------
1 | 1 | clientName | Google |
----------------------------------------------
2 | 1 | projectName |分析|
----------------------------------------------
3 | 1 |联系|夏普先生|
----------------------------------------------
4 | 1 | startDate | 29/12/2016 |
----------------------------------------------
Document表的文档名称和描述是使用Document中的隐藏字段创建的。
所以我可以创建文档没有问题。我有更新功能的问题。到目前为止,我有以下
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& ;&$ $!$$$$$$$$$$$ > value = $ value;
$ orgData-> update();
}
}
}
}
return View :: make($ document-> name.'Doc.edit',compact('项目','文件'));
}
所以,首先我得到我正在处理的文档,并将其存储在$ document 。然后我获取该文档的DocumentData并将其存储在$ docData中。 $ docData是一个包含文档的键值配对的集合。
然后我循环$ docData和输入的数据,键匹配在哪里,我重置它更新的值。
但是,目前,它会将所有内容更新为最后输入的数据字段。我不知道我可以在哪里执行更新操作,但我只需要更新它所指的行,而不是整个事情。
我怎么能去做这个吗?
谢谢
代码一点,并做了一些实现的更改。这个代码应该可以工作,所以让我们知道它是否没有,如果没有,那么失败了。
public function update请求$ request,Project $ project,$ id)
{
//你可以使用eager加载和查找关系查询
$ document = $ project-> document() - > ;与( 'documentData') - >找到($ ID);
//你可以只访问关系属性,不需要查询
$ docData = $ document-> documentData;
//获取所有输入,除了指定的键
$ input = $ request-> except(['filePath','documentType','documentTypeDesc']);
foreach($ docData as $ orgData){
//检查输入是否具有与docdata键匹配的键
if(array_key_exists($ orgData-> key, $ input)){
//更新值
$ orgData-> value = $ input [$ orgData-> key];
//使用save,不更新
$ orgData-> save();
}
}
return View :: make($ document-> name.'Doc.edit',compact('project','document'));
}
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
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'));
}
这篇关于Laravel 5 - 更新覆盖所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!