I'm encountering an error with the put method [![enter image description here][1]][1]
I'm using axios for post and fetch for put. I don't know how a put method will work on axios so if anyone could help I would really appreciate it.
Component.vue script
editItem (item) {
this.dialog = true
this.editedIndex = this.departments.indexOf(item)
this.id = item.id;
this.department = item.department_name;
},
save() {
const { department } = this
if (this.editedIndex > -1) {
fetch('/api/department', {
method: 'put',
body: JSON.stringify(this.department),
headers: {
'content-type': 'application/json'
}
})
} else {
axios
.post('/api/department', { department })
.then(response => this.initialize())
.catch(error => console.log(error))
}
this.close()
},
Controller
public function store(Request $request) {
$department = $request->isMethod('put') ? Department::findOrFail($request->id) : new Department;
$department->id = $request->input('id');
$department->department_name = $request->input('department');
if($department->save()) {
return response()->json($department);;
}
}
API
Route::get('departments', 'DepartmentController@index');
Route::get('department/{id}', 'DepartmentController@show');
Route::post('department', 'DepartmentController@store');
Route::put('department', 'DepartmentController@store');
Route::delete('department/{id}', 'DepartmentController@destroy');
The post method works but not for put as seen with the error posted above.
/api/departmentand the second is/api/department/