Home > Software design >  The DELETE method is not supported for this route. Supported methods: GET, HEAD
The DELETE method is not supported for this route. Supported methods: GET, HEAD

Time:10-02

My code is:

settings.blade.php (blade):

<form method="POST" action="{{route('user.settings', $apartment->apartment_id)}}">
    @method('delete')
    @csrf
    <button type="submit" >Delete</button>
    </form>

UserController.php:

public function delete(Apartment $apartment)
    {
    $apartment->delete();

    return redirect()->route('dashboards.users.index')
            ->withSuccess(__('Deleted successfully'));
    }

web.php:

//Authorization based on user role

Route::group(['prefix'=>'user', 'middleware'=>['isUser','auth','PreventBackHistory']], function(){
    Route::get('dashboard',[UserController::class,'index'])->name('user.dashboard');
    Route::delete('settings/{apartment}',[UserController::class,'delete'])->name('settings.delete');
    Route::resource('settings','UserController@delete')->name('user.settings');
    Route::get('profile',[UserController::class,'profile'])->name('user.profile');
    Route::get('settings',[UserController::class,'settings'])->name('user.settings');

});

Could someone please help?

CodePudding user response:

Route::resource('settings','UserController@delete')->name('user.settings');

You are re-routing the settings again using resource. Rename 'resource' to 'get'

Route::get('settings','UserController@delete')->name('user.settings');

Keep the same route and form.

Route::delete('settings/{apartment}',[UserController::class,'delete'])->name('settings.delete');
<form method="POST" action="{{route('settings.delete', $apartment->apartment_id)}}">
    @method('delete')
    @csrf
    <button type="submit" >Delete</button>
</form>

CodePudding user response:

Use route model binding

Route::delete('settings/{apartment}',[UserController::class,'delete'])->name('settings.delete');

Then

<form method="POST" action="{{route('settings.delete', $apartment->apartment_id)}}">
    @method('delete')
    @csrf
    <button type="submit" >Delete</button>
</form>
  • Related