I try to delete backup with DELETE method in Laravel and Axios but the request is comming back empty.
Controller
/**
* Remove the specified resource from storage.
* @param Request $request
*/
public function destroy(Request $request)
{
return $request->all();
}
Route
Route::middleware(['auth'])->prefix('backup')->group(function() {
Route::get('/', 'BackupController@index');
Route::delete('/delete', 'BackupController@destroy')->name('backup.delete');
});
Axios Request
axios.delete(`/backup/delete`,{data: {filename: filename}} )
.then((response)=>{
console.log(response.data)
})
.catch((err) => console.error(err.response.data.errors));
What is the problem with my code?
You send over axios the the filename. Then you have fetch in the destroy method the filename and get the model from the database.
Route
Route::delete('api/delete/{filename}', BackupController@destroy')->name('backup.delete');
Controller
$model = App\Models\Backup::where('filename', $request->filename)::first();
$model->destroy();
Sending the parameters via the body using the DELETE method is not recommended. Some servers do not support it. However.
axios.put(`/backup/delete`,{data: {filename: filename}} )
.then((response)=>{
console.log(response.data)
})
.catch((err) => console.error(err.response.data.errors));
Route::delete('/delete', 'BackupController@destroy')->name('backup.delete');
Then you can fetch in your Controller over the Request Object the parameter (filename).