[英]Laravel + Vuejs Input Form With Database Value and Update
Trying to do a simple input box. 试图做一个简单的输入框。 The default value should be a database value, and when user updates the value, it also updates the database.
默认值应该是一个数据库值,并且当用户更新该值时,它也会更新数据库。 I'm using Laravel 5.5 and this is a vue component.
我正在使用Laravel 5.5,这是一个vue组件。 So the initial value would be 3 from the database, but then if someone changes the value, it would also update the database.
因此,数据库的初始值为3,但是如果有人更改了该值,它也会更新数据库。 Am I on the right track with what's below, or am I way off?
我是否在正确的轨道上,下面是什么,还是我走了? Currently it won't get the initial amount, and it won't update.
目前,它不会获得初始金额,也不会更新。
<template>
<div>Corn: <input v-model="corn" style="width: 50px;" /></div>
</template>
<script>
export default {
data: function() {
return {
items: 'not updated',
corn: items.cornquant
} },
watch: { // whenever amount changes, function will run
corn: function(newCorn, oldCorn) {
this.corn = '2'
this.getCorn()
} },
mounted: function() {
this.getVueItems();
},
methods: {
getVueItems: function() {
axios.get('/testing').then(response => {
this.items = response.data;
}); },
getCorn: _.debounce(
function() {
this.corn = 'Thinking...'
var vm = this
axios.put('/corn/{amount}').then(response => {
vm.corn = response.data;
}) },
// milliseconds we wait for user to stop typing.
500
) }, }
</script>
And here's the route (did a little editing, this updates now): 这是路线(进行了一些编辑,现在更新):
Route::post('/corn', function () {
$test = App\Resource::where('user_id', Auth::id())->update(['cornquant' => request('amount')]);
return $test;
});
Use an es6 arrow function in debounce to preserve this
. 在反跳中使用es6箭头功能可保留
this
。 Then remove var vm = this
and assign to corn like this.corn = response.data
. 然后删除
var vm = this
并像this.corn = response.data
var vm = this
分配给corn。
And where are you initially calling getCorn
? 您最初在哪里打电话给
getCorn
?
Got everything sorted. 一切都整理了。 Defining default values was the hardest part, but ended up being easy enough!
定义默认值是最困难的部分,但最终变得很容易!
Here's the vue template file: 这是vue模板文件:
<template>
<div>Corn: <input v-model="corn" style="width: 50px;" /></div>
</template>
<script>
export default {
data: function() {
return {
items: 'not updated',
corn: '0'
} },
watch: { // whenever input amount changes, function will run
corn: function() {
this.getCorn()
} },
mounted: function() {
this.getVueItems(); //this will call the actual corn value to put as the default value
},
methods: {
getVueItems: function() {
axios.get('/testing').then(response => {
this.items = response.data;
this.corn = response.data.cornlq; //set initial value
}); },
getCorn: _.debounce(
function() {
var vm = this
axios.post('/corn', { //updates database
corn: this.corn,
}).then(response => {
vm.corn = response.data.cornlq; //keeps actual database value in input
}) },
2000
) }, }
</script>
And the route: 路线:
Route::post('/corn', function () {
App\Resource::where('user_id', Auth::id())->update(['cornlq' => request('corn')]); //update database with new amount
$result = App\Resource::where('user_id', Auth::id())->first(); //save all amounts to $result
return $result; //return result so I can update the vue
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.