vue.js - How to save category in laravel 5.7 with vue.js
问题描述
Using Laravel 5.7 with vuejs, I am trying to display parent_id from a MySQL categories table. I want to pass the name and get all it's child categories irrespective of the parent.
My blade
<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">category</label>
<select id="sub_category" v-model="parent_id" class="form-control">
<option data-display="main category" value="0">main category</option>
<option v-for="category in categories" :value="category.id">@{{ category.name }}</option>
</select>
</div>
<div class="form-group">
<button type="button" @click="addCategory()" class="btn btn-info">save</button>
</div>
</form>
web.php
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'],function (){
$this->get('panel', 'PanelController@index')->name('panel.index');
$this->resource('categories', 'CategoryController');
});
My vue
addCategory: function () {
axios.post(route('categories.store'), {
name: this.name,
parent_id: this.parent_id,
}).then(response => {
this.categories.push({'name': response.data.name, 'id': response.data.id});
}, response => {
this.error = 1;
console.log('Errors');
});
}
CategoryController
public function store(Request $request)
{
$category = new Category();
$category->name = $request->name;
$category->parent_id = $request->parent_id;
if ($category->save()) {
return $category;
}
}
I see this error in console for first
Too
And I get 405 error.
解决方案
@click
从提交按钮中删除。- 从表单操作中删除路线...并设置它#
- 添加
@submit="addCategory()"
到表单 - 在
axios.post
示例中添加没有路由功能的路由。
更新:
如果要防止页面刷新,请.prevent
在@submit
.
推荐阅读
- python - 来自多个数据帧的多个直方图在熊猫中合二为一
- x86 - 平面地址空间、线性地址、基地址、有效地址计算有什么区别
- java - 从不同的实体获取属性
- json - 使用 JSONSerialization 解析 YouTube json
- java - 将60FPS游戏克隆成四屏
- python - 如何使用python以字典格式合并多个函数的输出?
- c# - ASP.NET Core Razor PageModel ModelState 无效,因为 Id==0 的新导航属性
- python - 如何向网页添加内容?
- sql-server - 无法使用服务器名称连接 SQL Server
- python - 读取多个文件但在 python 中对一个文件执行摘要的优雅方式