Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

255
Views
DELETE method with request body in axios and laravel

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));

Console.log(response) enter image description here

What is the problem with my code?

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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();

Update

Sending the parameters via the body using the DELETE method is not recommended. Some servers do not support it. However.

  1. Change the method from delete to put or POST
axios.put(`/backup/delete`,{data: {filename: filename}} )
.then((response)=>{
      console.log(response.data)
   })
.catch((err) => console.error(err.response.data.errors));
  1. Adjust your Route
Route::delete('/delete', 'BackupController@destroy')->name('backup.delete');

Then you can fetch in your Controller over the Request Object the parameter (filename).

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs