为啥在 Laravel 中保存数据时出现方法不允许错误?
Posted
技术标签:
【中文标题】为啥在 Laravel 中保存数据时出现方法不允许错误?【英文标题】:Why am getting method not allowed error when saving the data in Laravel?为什么在 Laravel 中保存数据时出现方法不允许错误? 【发布时间】:2020-05-24 19:01:20 【问题描述】:在我的应用程序中,尝试保存数据时出现错误方法不允许。我在这里发布我的代码,请有人调查一下并帮助我。
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 错误?
【问题讨论】:
【参考方案1】:你的网址'/holiday/store'
剂量不多Route::resource('holiday', 'HolidayController');
修复
await this.$inertia.post('holiday', this.holiday)
检查路由及其对应的 URI
运行以下命令
php artisan route:list
【讨论】:
【参考方案2】:在您的 Vue 代码中,您应该使用对 '/holiday'
而不是 '/holiday/store'
的 POST 请求。
定义资源路由相当于:
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
【讨论】:
以上是关于为啥在 Laravel 中保存数据时出现方法不允许错误?的主要内容,如果未能解决你的问题,请参考以下文章