这是我将表单提交给vue资源的地方
<meta id="token" name="token" value="{{ csrf_token() }}">
<div class="modal fade" id="editroom" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Edit Blog Post</h4>
</div>
<div class="modal-body">
<form method="post" enctype="multipart/form-data" v-on:submit.prevent="updateRoom(fillRoom.id)">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Room Number:</label>
<input type="text" name="title" class="form-control" v-model="fillRoom.rn" />
</div>
<div class="form-group">
<label for="title">Status:</label>
<input type="text" name="title" class="form-control" v-model="fillRoom.status" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
这是我的vue-resource / ajax。调用路线
Vue.http.headers.common['X-CSRF-TOKEN'] = $("#token").attr("value");
new Vue({
el: "#app",
data:{
newRoom: {'rt': ''},
rooms: [],
dT: '',
fillRoom : {'rn':'','status':'', 'id': ''}
},
mounted: function() {
this.getRooms();
},
methods:{
editRoom: function(room){
this.fillRoom.rn = room.room_no;
this.fillRoom.status = room.status;
this.fillRoom.id = room.room_id;
$("#editroom").modal('show');
},
updateRoom: function(id) {
var input = this.fillRoom;
this.$http.patch('/admin_page/rooms/'+id,input).then(response => {
this.newRoom = {'rt': ''};
$("#edit-item").modal('hide');
toastr.success('Item Updated Successfully.', 'Success Alert', {timeOut: 5000});
}, response => {
console.log(response.date);
});
},
}
});
这是我调用控制器的路由资源
Route::group(['middleware' => ['web']], function() {
Route::get('/admin_page/rooms_page', 'RoomController@rooms_page');
Route::resource('admin_page/rooms', 'RoomController');
});
最后,这是我的控制器。它在更新后返回json数据
public function update(Request $request, $id)
{
$query = DB::table('rooms')
->where('id', $id)
->update(['room_no' => $request->input('rn')]);
return response()->json($query);
}