"Call to a member function store() on array" 使用 Laravel 8 和 Jetstream 上传文件时 - Vue & Intertia.js
"Call to a member function store() on array" When Uploading a File using Laravel 8 with Jetstream - Vue & Intertia.js
我在从 vue 文件中的表单上传图像时遇到问题。我尝试了几种方法来执行此操作,但似乎文件设置不正确。
我在表单标签中设置了“enctype="multipart/form-data"
这是我的输入元素:
<输入
@change="onFileChange"
类型=“文件”
接受=“图片/*”
class="表格控制"
名称=“文件”
id="文件"
aria-describedby =“helpId”
placeholder="上传文件"
/>
这是我的数据对象和发送数据的方法:
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;
}
},
我在控制器中接收数据并尝试存储文件:
public函数更新(请求$request,$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",
任何关于我错的想法都将不胜感激。
尝试在 FormData 中发送上传的文件。在 Vue 组件中定义一个方法来准备 FormData 以及您要通过 ajax 发送到服务器
的所有数据
prepareFormData() {
let data = new FormData;
Object.keys(this.professionaldevelopmentitem).forEach(
key => data.append(key, this.professionaldevelopmentitem[key]
);
return data;
}
然后使用此方法获取FormData并将其作为数据发送到addProfessionalDeveloomentItem
和updataProfessionalDevelopmentItems
中的服务器
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;
}
}
那你应该在$request key file $request->file('file')
下得到上传的文件
我在从 vue 文件中的表单上传图像时遇到问题。我尝试了几种方法来执行此操作,但似乎文件设置不正确。
我在表单标签中设置了“enctype="multipart/form-data"
这是我的输入元素:
<输入 @change="onFileChange" 类型=“文件” 接受=“图片/*” class="表格控制" 名称=“文件” id="文件" aria-describedby =“helpId” placeholder="上传文件" />
这是我的数据对象和发送数据的方法:
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; } },
我在控制器中接收数据并尝试存储文件:
public函数更新(请求$request,$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",
任何关于我错的想法都将不胜感激。
尝试在 FormData 中发送上传的文件。在 Vue 组件中定义一个方法来准备 FormData 以及您要通过 ajax 发送到服务器
的所有数据prepareFormData() {
let data = new FormData;
Object.keys(this.professionaldevelopmentitem).forEach(
key => data.append(key, this.professionaldevelopmentitem[key]
);
return data;
}
然后使用此方法获取FormData并将其作为数据发送到addProfessionalDeveloomentItem
和updataProfessionalDevelopmentItems
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;
}
}
那你应该在$request key file $request->file('file')
下得到上传的文件