Laravel Vue 通过隐藏字段获取信息



我需要将记录的用户 ID 传递给后端,我有 vuex 存储,这样我就可以获取我的用户信息,例如{{currentUser.id}}问题是我无法将其传递给后端它给了我验证错误,当我在表单中有此隐藏输入时需要user_id

<input type="hidden" name="user_id" :value="currentUser.id">

对于普通输入,我有像v-model="project.title"这样的v-model,无法在隐藏字段上使用。

这里的问题是如何将user_id传递到后端?

法典

<script>
import validate from 'validate.js';
export default {
data: function () {
return {
project: {
title: '',
body: '',
attachment: '',
projectclass: '',
deadline: '',
user_id: '',
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
},
errors: null
}
},
computed: {
currentUser() {
return this.$store.getters.currentUser;
}
},
methods: {
add() {
this.errors = null;
const errors = validate(this.$data.project);
if(errors) {
this.errors = errors;
return;
}
axios.post('/api/projects/new', this.$data.project)
.then((response) => {
this.$router.push('/projects');
});
}
}
}
</script>

发生这种情况是因为this.$data.project中的user_id没有得到更新。

与其隐藏输入,不如直接做

add() {
this.errors = null;
const errors = validate(Object.assign(this.$data.project, {user_id: this.currentUser.id}));
if(errors) {
this.errors = errors;
return;
}
axios.post('/api/projects/new', Object.assign(this.$data.project, {user_id: this.currentUser.id}))
.then((response) => {
this.$router.push('/projects');
});
}

最新更新