0
votes

I tried uploading multiple files into my database but I keep getting just one file, Am using laravel 5.5 all files show on my file directory path but store only one file into my database. Here is my code my view

<label for="image">{{ __(' image') }}</label>
 <input type="file" name="image[]" class="form-control" value="{{ $employee['image'] }}" multiple>

my route

Route::post('/people/employees/test/{id}', 'EmplController@test');

my controller

if($request->hasfile('image')){
        foreach ($request->image as $image) {
            $path = $image->getClientOriginalName();
            $filename = time() . '-' . $path;
$image->storeAs('employees', $employee->id . '/' . $filename);
            $image->move(public_path('employees'),$filename);
i inserted image here->$employee->image = $filename;
  $employee->save();

  }
}


1
Here, where you insert images to the employees table, which line? - sta
Try to create an array and store all of the image your upload and simply use json_encode you can easily store your multiple information into one cell and retreive them with json_decode() - MD. Jubair Mizan
i inserted images to the employees tables before saving to database like this $employee->image = $filename; then i saved $employee->save(); @STA - mr_ceejay
can you show me [email protected] - mr_ceejay

1 Answers

1
votes

Make an array, and implode it before insert to table, like :

$files = []; // an empty array
foreach($a as $b) {
   $files[] = $b->name; // insert name to array
}

$files = implode(",", $files); // insert $files into your table

So you need to change on your code :

if($request->hasfile('image')){
        $files = []; // make an array
        foreach ($request->image as $image) {
            $path = $image->getClientOriginalName();
            $filename = time() . '-' . $path;
            $files[] = $filename; // insert to array
$image->storeAs('employees', $employee->id . '/' . $filename);
            $image->move(public_path('employees'), $filename);
         }
         $files = implode(",", $files); // insert $files into your table
}