Laravel Spark图像文件上传字段问题

时间:2018-11-01 22:54:02

标签: php laravel vue.js laravel-spark

我正在迁移到Laravel Spark。我正在个人资料设置页面上尝试添加图片上传字段。由于配置文件使用VueJS,因此我不确定该如何处理。我已经按照文档添加了正常的文本字段,但是无法上传图片文件。

刀片表格

<!-- confirm_splash - Image File -->
<div class="form-group row justify-content-center">
    <div class="col-md-6 d-flex align-items-center">
        <div class="image-placeholder mr-4">
            <span role="img" class="profile-photo-preview" :style="previewStyle"></span>
        </div>
        <div class="spark-uploader mr-4">
            <input ref="confirm_splash" type="file" class="spark-uploader-control" name="confirm_splash"  :disabled="form.busy">
            <div class="btn btn-outline-dark">{{__('Update Photo')}}</div>
        </div>
        <span class="invalid-feedback" v-show="form.errors.has('confirm_splash')">
            @{{ form.errors.get('confirm_splash') }}
        </span>
    </div>
</div>

更新配置文件详细信息-VueJS组件 不确定是否需要在方法内部添加函数来获取图像文件。

Vue.component('update-profile-details', {
    props: ['user'],

    data() {
        return {
            form: new SparkForm({
                business_name: '',
                confirm_splash: ''
            })
        };
    },

    mounted() {
        this.form.confirm_splash = this.user.confirm_splash;
    },

    methods: {
        update() {
            Spark.put('/settings', this.form)
                .then(response => {
                    Bus.$emit('updateUser');
                });
        }
    }
});

个人资料详细信息控制器 最初在提交表单时不使用VueJS的情况。我正在使用作者ID将上传的图像添加到可管理的目录中。

public function update(Request $request)
{
    $this->validate($request, [
        'business_name' => 'required|min:1',
    ]);

    //Get the current user ID
    $data['id'] = Auth::user()->id;
    $authorID = $data['id'];
    $file = request()->file('confirm_splash');

    if($file) {
    $fileExtension = $file->extension();
    $unique_name = md5($file. time()).'.'.$fileExtension;

    $fileImg = $file->storeAs('/public/profile/' . $authorID, $unique_name);
    $filePub = '/profile/'. $authorID."/".$unique_name;

    };

    if(!$file) {
      $filePub = '';
    }

    $request->user()->forceFill([
        'business_name' => $request->business_name,
        'confirm_splash' => $filePub,
    ])->save();
}

0 个答案:

没有答案
相关问题