0

I have an Accessor that sets a file name and path for an uploaded image, and a mutator to retrieve the photo url. It works on single file upload, but i cannot think of a way to convert it for multiple file upload for a user gallery.

this is my input in my blade

<input type="file" id="deal_img_input" class="form-control" name="featured_image> 

this is my model

const PATH_PREFIX = 'public/deals';

public function getPhotoNameAttribute()
{
    if ($this->id && $this->featured_photo_extension) {
        return "$this->id.$this->featured_photo_extension";
    }

    return null;
}

public function getPhotoPathAttribute()
{
    if ($name = $this->getPhotoNameAttribute()) {
        return self::PATH_PREFIX . "/$name";
    }

    return null;
}

public function getPhotoUrlAttribute()
{
    $path = $this->getPhotoPathAttribute();
    if (Storage::exists($path)) {
        return Storage::url($path) . '?t=' . Storage::lastModified($path);
    }

    return '/assets/app/store/img/placeholder.jpg';

}

public function setPhotoExtensionAttribute($value)
{
    $path = $this->getPhotoPathAttribute();

    if (Storage::exists($path)) {
        Storage::delete($path);
    }

    $this->attributes['photo_extension'] = $value;
}

this is my controller

    if ($photo_featured = $request->file('feautured_image')) {
        $deals->featured_photo_alt = 'Featured Photo';
        $deals->featured_photo_extension = $photo_featured->extension();
        $photo_featured->storeAs(Deal::PATH_PREFIX, $deals->photo_name);

    }
    $deals->save();

How can i modify the code if i'll have multiple inputs?

<input type="file" class="form-control" name="gallery_image[]"> 
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
eeunice
  • 1
  • 1

1 Answers1

0

Did you try to add the "multiple" attribute to your input ?

<input type="file" class="form-control" name="gallery_image" multiple="multiple">

https://stackoverflow.com/a/1593259/9291504

  • yes, but what happens is only the first input is saved – eeunice Apr 17 '19 at 08:50
  • php.net/manual/en/features.file-upload.multiple.php should bring an answer, since Laravel extends SplFileInfo. So i guess that `$request->file('feautured_image')` should return an array – Arnaud Brenier Apr 17 '19 at 09:13