0

在我的应用程序中,尝试保存数据时出现错误方法不允许。我在这里发布我的代码,请有人调查一下并帮助我。

HolidayAdd.vue

<template>
    <layout>
        <form @submit.prevent="handleSubmit">
            <div class="input-group">
                <div class="input-group-prepend">
                    <span for="name" class="input-group-text">First Name and Last Name </span>
                </div>
                <input type="text" class="form-control" name="firstname" placeholder="Enter your First Name" v-model="holiday.fname" id="fname">
                <input type="text" class="form-control" name="lastname" placeholder="Enter your Last Name" v-model="holiday.lname" id="lname">
            </div>
            <br>
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">Start Date </span>
                </div>
                <input type="date" class="form-control" name="startdate" v-model="holiday.sdate" id="sdate">
            </div>
            <br>
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">End Date</span>
                </div>
                <input type="date" class="form-control" name="enddate" v-model="holiday.edate" id="edate">
            </div>
            <br>
            <button type="submit" class="btn btn-info">Apply</button>
        </form>
    </layout>
</template>

<script>
import Layout from './../../Shared/Layout'
export default {
    components: {
        Layout
    },
    data() {
        return {
            holiday: {
                fname: '',
                lname: '',
                sdate: '',
                edate: ''
            }
        }
    },
    methods: {
        async handleSubmit() {
            let response = await this.$inertia.post('/holiday/store', this.holiday)
        }
    }
}
</script>

假日控制器.php

public function store(Request $request)
    {

        $holiday = $request->validate([
            'firstname' => 'required',
            'lastname' => 'required',
            'startdate' => 'required',
            'enddate' => 'required'
        ]);

        Holiday::create($holiday);

        return redirect()->route('holiday.index')->with('success', 'Record Inserted Successfully');
    }

网页.php

Route::resource('holiday', 'HolidayController');

据我所知,没有错误,那为什么我在这里收到 405 错误?

4

2 回答 2

2

你的网址'/holiday/store'剂量不大Route::resource('holiday', 'HolidayController');

使固定

await this.$inertia.post('holiday', this.holiday)

检查路由及其对应的 URI

运行以下命令

php artisan route:list
于 2020-02-09T05:53:12.110 回答
2

在您的 Vue 代码中,您应该使用 POST 请求'/holiday'而不是'/holiday/store'.

定义资源路由等价于:

Route::get('/holiday', 'HolidayController@index');
Route::get('/holiday/create', 'HolidayController@create');
Route::post('/holiday', 'HolidayController@store');
Route::get('/holiday/{holiday}', 'HolidayController@show');
Route::get('/holiday/{holiday}/edit', 'HolidayController@edit');
Route::put('/holiday/{holiday}', 'HolidayController@update');
Route::patch('/holiday/{holiday}', 'HolidayController@update');
Route::delete('/holiday/{holiday}', 'HolidayController@destroy');

https://laravel.com/docs/5.8/controllers#resource-controllers

于 2020-02-09T05:53:49.000 回答