Looking at your code, I would figure that it will only work when you upload BOTH files, and will fail every time you upload 1 single file, is that correct ?
It's because of this part:
$filenames = [];
// Check die files
if($request->file('aandeel_pop'))
$filenames[] = $request->file('aandeel_pop');
if($request->file('nfc_pop'))
$filenames[] = $request->file('nfc_pop'); // Vind die user $user = Auth::user();
foreach($filenames as $file) {
if(!empty($file)){
$filename = time() . $file->getClientOriginalName();
$file->move(storage_path('/app/public/pop/uploads/'), $filename);
$filenames[] = $filename;
}
}
You are reusing that $filenames array for both the files, and their filenames after uploading.
And a few lines down:
$transaksie->aandeel_pop = $filenames[2];
$transaksie->nfc_pop = $filenames[3];
You now expect both files to occupy index 2 and 3 (but if you only uploaded 1 file, the $filenames array would only have 2 values, key 0 = the uploaded file, and key 1 = the filename after upload.)
Refactor this part into:
$files = [];
$filenames = [];
// Check die files
if($request->file('aandeel_pop'))
$files ['aandeel_pop'] = $request->file('aandeel_pop');
if($request->file('nfc_pop'))
$files ['nfc_pop'] = $request->file('nfc_pop'); // Vind die user $user = Auth::user();
foreach($files as $type => $file) {
if(!empty($file)){
$filename = time() . $file->getClientOriginalName();
$file->move(storage_path('/app/public/pop/uploads/'), $filename);
$filenames[$type] = $filename;
}
}
and the second part:
if(!empty($filenames)) {
$betaling->trans_nommer = $request->trans_nommer;
$betaling->user_id = $user->lid_nommer;
if(isset($filenames['aandeel_pop'])
$betaling->aandeel_pop = $filenames['aandeel_pop'];
if(isset($filenames['nfc_pop'])
$betaling->nfc_pop = $filenames['nfc_pop'];
$betaling->uploaded = Carbon::now();
$betaling->save();
}
Now it will only grab the files that have actually been uploaded. (Make srure that $betaling could be saved without those files, or that you have your upload form (and validation) make sure the files are being uploaded.
ps. Is this African ? Looks so much like my native dutch, Makes me think 'Die Antwoord' has been coding :)