hello im trying to upload multiple file on laravel. im following my others single upload file laravel. but when im trying to submit, it says an error
Call to a member function getClientOriginalName() on array
i know the problem, but i dont know how to solved it.
here’s my blade view
<div class="row pb-3">
<div class="col-sm-4"><label>Akta Lain Lain </label></div>
<div class="col-sm-8">
<div class="custom-file" style="width: 500px; height: 40px; cursor: pointer;">
<input type="file" class="custom-file-input" name="ALL[]" multiple>
<label class="custom-file-label">Choose file</label>
</div>
</div>
</div>
and here’s my controller
$deb = new Debitur_Badan_Usaha();
if ($request->hasFile('ALL')) {
$files = [];
foreach ($request->file('ALL') as $items) {
if ($items->isValid()) {
$name_all = time().'_'.rand(1,9999).'_'.$request->ALL->getClientOriginalName();
$items->move('data_debitur/', $name_all);
$files [] = $name_all;
}
}
$deb->AKTE_LAIN_LAIN = $files;
$deb->save();
}
i think the problem bcs on the name blade file is ALL[] but the getclientoriginalname doesnt accept an array? does anyone know how to solved it so it can be uploading multiple image? thank u
updated controller
$deb = new Debitur_Badan_Usaha();
if ($request->hasFile('ALL')) {
$files = [];
foreach ($request->ALL as $file) {
if ($file->isValid()) {
$name_all = time().'_'.rand(1,9999).'_'.$file->getClientOriginalName();
$file->move('data_debitur/', $name_all);
array_push($files, $name_all);
}
}
$deb->AKTE_LAIN_LAIN = $files;
$deb->save();
}
2
Answers
You were close, you already have a foreach loop to deal with the array of files, all you need to do is to now utilize it and use it, here is how:
Note: I changed items -> file, then for getting the original name, you can call that directly on the file.
Extra: you can also get the extension like
$file->getClientOriginalExtension();
Try this one: