首页 文章

如何使用vue.js在laravel 5.7中保存类别

提问于
浏览
0

使用Laravel 5.7和vuejs,我试图从MySQL类别表中显示parent_id . 我想传递这个名字并获得所有它的子类别而不管父母 .

我的刀片

<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');
});

我的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;
    }
}

我先在控制台中看到这个错误

Vue js

VVue

我得到405错误 .

405 error

1 回答

  • 1
    • 从提交按钮中删除 @click .

    • 从表单操作中删除路由...并设置它#

    • @submit="addCategory()" 添加到表单中

    • axios.post 中充分添加没有路由功能的路由 .

    Update: 如果要阻止页面刷新,请在 @submit 之后添加 .prevent .

相关问题