Home > Enterprise >  laravel multiple upload getClientOriginalName on array
laravel multiple upload getClientOriginalName on array

Time:01-13

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 >
    <div ><label>Akta Lain Lain </label></div>
    <div >
        <div  style="width: 500px; height: 40px; cursor: pointer;">
            <input type="file"  name="ALL[]" multiple>
            <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();
}

CodePudding user response:

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.

if ($request->hasFile('ALL')) {

    //foreach file request
    foreach($request->ALL as $file) {
        if ($file->isValid()) {
            //here we will call the file originalname
            $name_all = time().'_'.rand(1,9999).'_'.$file->getClientOriginalName();
            //moving the file
            $file->move('data_debitur/', $name_all);

            //we add the file to the field and save it
            $deb->AKTE_LAIN_LAIN = $name_all;
            $deb->save();
        }
    }
}

Extra: you can also get the extension like $file->getClientOriginalExtension();

CodePudding user response:

Try this one:

 $deb = new Debitur_Badan_Usaha();
        $files = [];
           foreach ($request->file('ALL') as $item) {
              if ($item->isValid()) {
                $name = time().'_'.rand(1,9999).'_'.$item->getClientOriginalName();
                $item->move('data_debitur/', $name);
                $files [] = $name;
               }
            }
                $deb->AKTE_LAIN_LAIN = $files;
                $deb->save();
  • Related