提问者:小点点

使用带有Jetstream-Vue的Laravel 8上载文件时“调用数组上的成员函数store()”


我从vue文件中的表单上载图像时遇到问题。我尝试了几种方法,但似乎文件设置不正确。

>

这是我的输入元素:

以下是我的数据对象和发送数据的方法:

  data() {
      return {
    editMode: false,
    professionaldevelopmentitems: [],
    professionaldevelopmentitem: {
      domain: 1,
      domaincategory: 1,
      title: "",
      dateofpd: "",
      location: "",
      lengthofpd: "",
      facilitatorname: "",
      facilitatorcredentials: "",
      reflection: "",
      file: "",
    },
  };
},
methods: {
  onFileChange(e) {
    alert(e.target.files[0]);
    alert(e.target.files[0].name);
    var files = e.target.files || e.dataTransfer.files;
    if (!files.length)
      return;
    this.professionaldevelopmentitem.file = e.target.files[0];
    alert(this.professionaldevelopmentitem.file);
  },    
  async addProfessionalDevelopmentItem() {
    document.getElementById("pdForm").reset();
    this.editMode = false;
    const res = await axios.post(
      "/api/professionaldevelopmentitems",
      this.professionaldevelopmentitem
    );

    if (res.status === 201) {
      Toast.fire({
        icon: "success",
        title: res.data,
      });
      document.getElementById("pdForm").reset();
      $("#manageProfessionalDevelopmentItem").modal("hide");
      Fire.$emit("modifiedPDItem");
    }
  },
  async editProfessionalDevelopmentItem(data) {
    this.professionaldevelopmentitem = Object.assign({}, data);
    this.editMode = true;
  },
  async updateProfessionalDevelopmentItems(data) {
    const res = await axios.put(
      `/api/professionaldevelopmentitems/${data.id}`,
      this.professionaldevelopmentitem
    );

    if (res.status === 200) {
      Toast.fire({
        icon: "success",
        title: res.data,
      });
      document.getElementById("pdForm").reset();
      $("#manageProfessionalDevelopmentItem").modal("hide");
      Fire.$emit("modifiedPDItem");
      this.editMode = false;
    }
  },

我在控制器中接收数据,并尝试存储文件:

公共函数更新(请求$请求,$id){

dd($request->all());

$this->validate($request, [
    'title' => ['required'],
    'dateofpd' => ['required'],
    'lengthofpd' => ['required'],
    'location' => ['required']
]);

$path = $request->file('filename')->store('uploads');

$pditem = ProfessionalDevelopmentItem::find($id);
$pditem->domain = $request->domain;
$pditem->domaincategory = $request->domaincategory;
$pditem->title = $request->title;
$pditem->dateofpd = $request->dateofpd;
$pditem->lengthofpd = $request->lengthofpd;
$pditem->location = $request->location;
$pditem->facilitatorname = $request->facilitatorname;
$pditem->facilitatorcredentials = $request->facilitatorcredentials;
$pditem->certificategranted = $request->certificategranted;
$pditem->certificateexpires = $request->certificateexpires;
$pditem->certificateexpiration = $request->certificateexpiration;
$pditem->reflection = $request->reflection;
$pditem->nameofinstitution = $request->nameofinstitution;
$pditem->coursename = $request->coursename;
$pditem->coursecode = $request->coursecode;
$pditem->hoursofinstruction = $request->hoursofinstruction;
$pditem->creditgranted = $request->creditgranted;
$pditem->bookname = $request->bookname;
$pditem->bookauthor = $request->bookauthor;
$pditem->bookyear = $request->bookyear;
$pditem->bookpublisher = $request->bookpublisher;
$pditem->otherdescription = $request->otherdescription;
$pditem->filename = $path;

$pditem->save();

return response('Successfully Updated the Professional Development Item.', 200);

}

尝试存储文件时,返回的响应是一个错误:

"message": "Call to a member function store() on array",
"exception": "Error",

任何关于我错在哪里的想法都将不胜感激。


共1个答案

匿名用户

尝试在FormData中发送上载的文件。在Vue组件中定义一个方法,以准备FormData,其中包含要通过ajax发送到服务器的所有数据

prepareFormData() {
    let data = new FormData;

    Object.keys(this.professionaldevelopmentitem).forEach(
        key => data.append(key, this.professionaldevelopmentitem[key]
    );

    return data;
}

然后使用此方法获取FormData,并将其作为数据发送到add专业人员DeveloomentItemupdata专业人员开发项目中的服务器

async addProfessionalDevelopmentItem() {
    document.getElementById("pdForm").reset();
    this.editMode = false;
    const res = await axios.post(
      "/api/professionaldevelopmentitems",
      this.prepareFormData()
    );

    if (res.status === 201) {
      Toast.fire({
        icon: "success",
        title: res.data,
      });
      document.getElementById("pdForm").reset();
      $("#manageProfessionalDevelopmentItem").modal("hide");
      Fire.$emit("modifiedPDItem");
    }
  },
  

  async updateProfessionalDevelopmentItems(data) {
    const res = await axios.put(
      `/api/professionaldevelopmentitems/${data.id}`,
      this.prepareFormData()
    );

    if (res.status === 200) {
      Toast.fire({
        icon: "success",
        title: res.data,
      });
      document.getElementById("pdForm").reset();
      $("#manageProfessionalDevelopmentItem").modal("hide");
      Fire.$emit("modifiedPDItem");
      this.editMode = false;
    }
}

然后,您应该在密钥文件$请求下的$请求中获取上传的文件-