为什么在 Laravel 中保存数据时出现 method not allowed 错误?
Why am getting method not allowed error when saving the data in Laravel?
在我的应用程序中,尝试保存数据时出现错误方法不允许。我在这里发布我的代码,请有人调查一下并帮助我。
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>
HolidayController.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');
}
web.php
Route::resource('holiday', 'HolidayController');
据我所知,没有错误,那为什么我在这里收到405错误?
你的url'/holiday/store'
剂量不多Route::resource('holiday', 'HolidayController');
修复
await this.$inertia.post('holiday', this.holiday)
检查路由及其对应的 URI
运行以下命令
php artisan route:list
在您的 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
在我的应用程序中,尝试保存数据时出现错误方法不允许。我在这里发布我的代码,请有人调查一下并帮助我。
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>
HolidayController.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');
}
web.php
Route::resource('holiday', 'HolidayController');
据我所知,没有错误,那为什么我在这里收到405错误?
你的url'/holiday/store'
剂量不多Route::resource('holiday', 'HolidayController');
修复
await this.$inertia.post('holiday', this.holiday)
检查路由及其对应的 URI
运行以下命令
php artisan route:list
在您的 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