我想首先使用vue.js和laravel 5.7创建一个简单的类别。我试图做那些时间,但我没有成功
如何使用vue.js保存名称?
提示:我没有看到任何错误。我想将名称保存在数据库中。
index.blade.php
<form action="{{ route('categories.store') }}" method="post">
@csrf
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" class="form-control" v-model="name">
</div>
<div class="form-group">
<label for="sub_category">Sub Category:</label>
<select id="sub_category" v-model="parent_id" class="form-control">
</select>
</div>
<div class="form-group">
<button type="button" @click="addCategory()" class="btn btn-info">Save</button>
</div>
</form>
app.js
require('./bootstrap');
window.Vue = require('vue');
Vue.component('example-component', require('./components/ExampleComponent.vue'));
const app = new Vue({
el: '#app',
data: {
name: "",
parent_id: ""
},
methods: {
addCategory: function () {
axios.post('/admin/store', {
name: this.name,
parent_id: this.parent_id
}).then(response => {
}, response => {
this.error = 1;
console.log('Errors')
})
}
}
});
CategoryController
public function store(Request $request)
{
Category::create([
'name' => $request->name
]);
return response('Category created', 201);
}
web.php
Route::namespace('Admin')->prefix('admin')->group(function (){
$this->get('panel', 'PanelController@index')->name('panel.index');
$this->resource('categories', 'CategoryController');
});