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[]">