mercredi 16 décembre 2015

Data in loop is repeated

I am trying to work something out. I have the following

$fileString = "";
if (Input::hasFile('filePath')) {
    $files = Input::file('filePath');
    dd($files);
    foreach($files as $file) {
        $file->move(public_path('uploads'), $file->getClientOriginalName());

        $fileString .= public_path('uploads') . '/' . $file->getClientOriginalName();

        $uploadDoc = new ReportingDocUpload();
        $uploadDoc->filename = $file->getClientOriginalName();
        $uploadDoc->mime = $file->getClientOriginalExtension();
        $uploadDoc->filepath = $fileString;
        $uploadDoc->documentId = $dsReportingDoc->id;

        $uploadDoc->save();
    }
}

For each uploaded file, it should create a new row in the database. The dd I have displays something like the following if I upload 3 documents

array:3 [▼
  0 => UploadedFile {#29 ▶}
  1 => UploadedFile {#30 ▶}
  2 => UploadedFile {#31 ▶}
]

So this all looks good. So, I have a loop which should loop each UploadedFile and save it to the database.

At the moment, this kind of works. A new row is added for each uploaded document, and everything besides one thing looks correct.

The thing that is wrong is the filePath. For the first row, this is fine. The second row in the database contains the filePath for both the first file and the second file. And the third row shows the filePath for all three rows. This should only contain the filePath for the current file.

I have looked over it, and not sure what I am missing, but for some reason this is occuring.

Am I missing something obvious here?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire