首页 > 解决方案 > 通过 api 发送的 vuejs 表单数据在 laravel 控制器上不可见

问题描述

看一看

Vue.js

data() {
    return {

     url: '/api/client/document/upload',

        }
    },

计算的

attachment() {
            return  {
                slug: 'testing',
                test_type[enter image description here][1]: (this.individual === 1)? 'transfer_individual': 'transfer_corporate',
                application_id: this.client_investment_id
            };

上传方式

upload: function () {

            if(!this.validateForm()) {
                Message.warning({ message: 'Complete the form and proceed' });
                return;
            }

            if(!this.$refs.upload.uploadFiles[0]) {
                Message.warning({ message: 'Upload the form and proceed' });
                return;
            }

            console.log('data', this.attachment);

            this.$refs.upload.submit();
        },

控制器端 laravel

 public function uploadDocument()
 {
    $input = request()->all();

    dd($input);
 }

有一个文件正在上传到给定的 url;

当我从控制器 dd 时,我得到一个 null 的 application_id 但如果我在提交之前 console.log 能够查看我的数据。我可能做错了什么。

console.log 输出 dd 输出

标签: laravelvue.jslaravel-5.6

解决方案


将请求作为参数传递给函数并all()在请求实例上调用方法。

 public function uploadDocument(Request $request)
 {
     $input = $request->all();

     dd($input);
 }

要在 public/images 文件夹中获取并保存名称与时间戳连接的文件,请尝试

//Assuming you have the file input name as `photo` 
if ($file = $request->file('photo')) {

  $name = time() . $file->getClientOriginalName();
  $file->move('images/', $name);

}

推荐阅读