2016-04-18 18 views
0

データを上書きする際に問題が発生しています。私はDocument ModelとDocumentData Modelを持っています。ドキュメントは多くの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

1.リレーションシップを正しく設定すると、 '$ documentData-> documentId = $ document-> id;'は不要です。 '$ documentData-> document() - > saveMany()' - 2.私は[Eloquentのドキュメントを読む](https://laravel.com/docs/master/eloquent-relationships#introduction)をお勧めします。それほど遠くない。 3。入力データとプロパティの設定をループすることはお勧めしません。 – ash

+1

私はあなたの質問を正しく理解しているのかどうか分かりませんが、 'DocumentData'の複数のインスタンスを作成しようとしていますか?そうであれば間違った方向に行きます。最終的なコードスニペットは、アップロードされたdbごとに新しいDocumentDataエントリを作成しません。最後にアップロードされたファイルのみを表示するのと同じエントリに書き込みを続けます。複数のインスタンスを作成するには、forループ内に新しいインスタンスを新規作成する必要があります。いずれにしても、灰が推奨する関係を参照するようにする必要があります。 – haakym

答えて

1

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

、その後、あなたが設定することができます。私が理解として、あなたは、まずDocumentクラスの関係メソッドを追加することで、このため一対多関係を設定することができますので、Documentは多くのDocumentData Sを持つことができます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. もし助けが必要なコード内のいくつかのミスがあるかもしれませんこの回答はあなたのためにうまくいきますが、最初のアプローチがなぜそうでなかったかを理解するためには、より多くの があるかもしれません! アップロードしたファイルごとにDBに新しいエントリが作成されず、その他のマイナーな問題がいくつか追加されました。
  3. よくわかりますが、彼らは実際に 強力ですし、 を把握できても、頭痛の悩みをたくさん節約します。これは、DBのやりとりをはるかに簡単にするでしょう!

ハッピーコーディング!