繁体   English   中英

使用Accessors&Mutators进行Laravel多文件上传

[英]Laravel multiple file upload using Accessors & Mutators

我有一个Accessor,它为上传的图像设置文件名和路径,还有一个mutator来检索照片网址。 它适用于单个文件上传,但我想不出一种方法来转换它为用户库的多个文件上传。

这是我在刀片上的输入

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

这是我的模特

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;
}

这是我的控制器

    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();

如果我有多个输入,我该如何修改代码?

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

您是否尝试在输入中添加“multiple”属性?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM