2016-04-18 101 views
0

我遇到数据被覆盖的问题。我有一个文档模型和一个DocumentData模型。一个Document可以有很多DocumentData。 所以我有一个数据采集形式,在我的创造功能,如果我输出的要求,我得到类似如下的Laravel 5 - 保存的数据被覆盖

array:8 [▼ 
    "_token" => "UHTN66xH4ChHpaxWAt4hWYFfpwUyjo6EunLp2iuV" 
    "whatData" => "rgfterter" 
    "whoData" => "tertertertert" 
    "startDate" => "28-04-2016" 
    "deliveryDate" => "30-04-2016" 
    "whyData" => "wefrwerwe" 
    "howData" => "rwerwerwer" 
    "filePath" => array:2 [▼ 
    0 => UploadedFile {#30 ▼ 
     -test: false 
     -originalName: "image.png" 
     -mimeType: "image/png" 
     -size: 788038 
     -error: 0 
    } 
    1 => UploadedFile {#31 ▼ 
     -test: false 
     -originalName: "image2.png" 
     -mimeType: "image/png" 
     -size: 1091127 
     -error: 0 
    } 
    ] 
] 

那么,什么我需要做的就是创建一个文档

$document = new Document(); 
$document->projectId = $project->id; 
$document->name = 'Test Document'; 
$document->save(); 

而且然后使用上述数据执行documentData。 DocumentData基本上有一个键/值,其中键是输入标签,值是输入数据。 目前我在做这个

$documentData = new DocumentData(); 
$documentData->documentId = $document->id; 

foreach ($inputs as $key => $value) { 
    if($key !== '_token' && $key !== 'filePath') { 
     $documentData->key = $key; 
     $documentData->value = $value; 
    } 
    $documentData->save(); 
} 

$fileString = ""; 
if (Input::hasFile('filePath')) { 
    $files = Input::file('filePath'); 

    foreach($files as $file) { 
     $file->move(public_path('uploads'), $file->getClientOriginalName()); 

     $fileString .= public_path('uploads') . '/' . $file->getClientOriginalName(); 
     $documentData->key = 'File Path'; 
     $documentData->value = $fileString; 
     $documentData->save(); 
    } 
} 

被创建的文档很好,但我只得到了DocumentData一行 - 最后上传的文件路径。所以看起来其他行正在被覆盖。 如何确保所有输入在我的数据库中正确记录?

谢谢

+1

1.'$ documentData-> documentId = $ document-> id;'如果您设置正确的关系是不必要的;你可以这样做:'$ documentData-> document() - > saveMany()' - 2.我推荐[阅读Eloquent文档](https://laravel.com/docs/master/eloquent-relationships#introduction),因为你不是那么遥远。 3。我不建议你循环输入数据和设置属性,更明确会更好。 – ash

+1

我不确定我是否正确地理解了你的问题,但是你是否想要创建多个'DocumentData'实例,如果是的话,那么你会以错误的方式去解决它。您的最终代码片段不会在每个上传文件的数据库中创建一个新的“DocumentData”条目,它只会继续写入相同的条目,这就是为什么您只能看到上次上传的文件。您需要在for循环中新建一个新实例,以创建多个实例。无论如何,你应该像灰建议查找关系一样。 – haakym

回答

1

为了使事情更容易在你的Eloquent模型中建立一些关系。据我所知,一个Document可以有很多DocumentData S左右,你可以通过在Document类,首先添加关系法设立这个一个一对多关系:

public function documentData() 
{ 
    // you should include the full namespace here 
    return $this->hasMany(App\DocumentData::class); 
} 

,然后你可以设置关系对DocumentData类的另一面:

public function document() 
{ 
    // again, you should include the full namespace here 
    return $this->belongsTo(App\Document::class); 
} 

现在,您可以访问和创建使用这些关系的方法!

我要重写一下(我的假设是)您的控制器操作在这里进行一些调整,让我知道如果您没有关注。

public function storeDocument(Request $request) 
{ 
    // create new document 
    $document = new Document(); 
    $document->name = 'Test Document'; 
    // you could probably set up a relationship for this too! 
    $document->projectId = $project->id; 
    $document->save(); 

    // grab the uploaded files 
    //we can use this to loop through and create a DataDocument per uploaded file 
    // this assumes uploading a file is required, otherwise it won't work! 
    $uploadedFiles = $request->file('filePath'); 

    // loop through each file 
    foreach($uploadedFiles as $file) { 
     if ($file->isValid()) { 
      // upload file logic 
      $extension = $file->getClientOriginalExtension(); 
      $fileName = uniqid() . '.' . $extension; // give the file a unique name 
      $file->move(public_path() . '/uploads', $fileName); 

      // create our new DocumentData related to our Document 
      // using our documentData() relationship 
      $document->documentData()->create([ 
       'whatData'  => $request->get('whatData'), 
       'whoData'  => $request->get('whoData'), 
       'startDate' => $request->get('startDate'), 
       'deliveryDate' => $request->get('deliveryDate'), 
       'whyData'  => $request->get('whyData'), 
       'howData'  => $request->get('howData'), 
       'filePath'  => public_path() . '/uploads' . $fileName, 
      ]); 

      // finish up! 
      dd($document); 
      // dd() so you can see what you've created, 
      // of course you should probably return redirect here 
     } 
    } 

} 

注意事项:

  1. 有可能是在代码中的一些错误,因为我没有测试过, 请做出评论,如果是这样的话,你需要帮助
  2. 如果这个答案适用于你,这很好,但它可能更重要,以了解为什么你的初始方法没有!您 未在您的数据库中为您上传的每个文件创建新条目 以及其他一些小问题。
  3. 非常熟悉口才和人际关系,他们真的很强大,并且即使你能掌握 ,它也会为你节省很多头痛,它会使你的数据库交互更加简单!

快乐编码!