abokor hassan
abokor hassan

Reputation: 357

laravel: problem in delete Route function

I'm working on Laravel5.5, and i'm doing the Crud operation. currently i'm working on deleting, but i got Route problem issue.

this is the Routes for crud operations of controll

    // Test Management
Route::group([ 'prefix' => 'tests'], function () {
    Route::get('data', 'TestController@data')->name('tests.data');
    Route::get('{tests}/delete', 'TestController@destroy')->name('tests.delete');
    Route::get('{tests}/confirm-delete', 'TestController@getModelDelete')->name('tests.confirm-delete');

});
Route::resource('tests', 'TestController');

and this is where the links to the delete function comes where the user confirms the delete butttom

public function getModelDelete(Test $test)
{
    $model = 'tes'; # this model is for the path of the modal
    $confirm_route = $error = null;
    try {
        $confirm_route = route('admin.tests.delete', ['id' => $test->id]);
        return view('admin.layouts.modal_confirmation', compact('error', 'model', 'confirm_route'));
    } catch (GroupNotFoundException $e) {

        $error = trans('tes/message.error.destroy', compact('id'));
        return view('admin.layouts.modal_confirmation', compact('error', 'model', 'confirm_route'));
    }
}

this is the modal for confiming delete

<div class="modal-header">
   <button type="button" class="close" data-dismiss="modal" aria- 
       hidden="true">&times;</button>
    <h4 class="modal-title" 
        id="user_delete_confirm_title">@lang($model.'/modal.title')</h4>
</div>
<div class="modal-body">
    @if($error)
       <div>{!! $error !!}</div>
    @else
       @lang($model.'/modal.body')
    @endif
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data- 
          dismiss="modal">@lang($model.'/modal.cancel')</button>
     @if(!$error)
        <a href="{{ $confirm_route }}" type="button" class="btn btn- 
              danger">@lang($model.'/modal.confirm')</a>
     @endif
</div>

and this is the delete method in controller

 public function destroy($id)
{
    $test = Test::find($id);
    $test->delete();
    return redirect('admin/tests')->with('success', trans('tes/message.success.delete'));

    }
}

The thing is when i hit confirm delete buttom it says Page not found and this is url it will go

http://localhost/bsproject/public/index.php/admin/tests//delete

and i noticed that there's extra backslash. and thank you!

Upvotes: 0

Views: 321

Answers (1)

thisiskelvin
thisiskelvin

Reputation: 4202

When using the route() helper, passing a variable must have a key of the url parameter.

For your case, instead of id you should use tests:

$confirm_route = route('admin.tests.delete', ['tests' => $test->id]);

I would advise changing the route to id so it makes more sense:

Route::get('{id}/delete', 'TestController@destroy')->name('tests.delete');

Site note: It would be best to use Route::delete() to be clear with your routes. ::get() assumes you are getting a page or data. This will require changing your modal to use a <form> which wraps your <button>.

Upvotes: 1

Related Questions