skip to Main Content

My code is:

settings.blade.php (blade):

<form method="POST" action="{{route('user.settings', $apartment->apartment_id)}}">
    @method('delete')
    @csrf
    <button type="submit" class="btn btn-danger btn-sm">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?

2

Answers


  1. 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" class="btn btn-danger btn-sm">Delete</button>
    </form>
    
    Login or Signup to reply.
  2. 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" class="btn btn-danger btn-sm">Delete</button>
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search