skip to Main Content

I have a route which has two parameters.

Route::delete('/org/{org-id}/delete/user/{user-id}', 'AppHttpControllersOrganizationController@deleteUserFromOrg')
->name('delete-user-from-org');

i call it in view

 <td><a href="{{route('delete-user-from-org', ['org-id'=>$data->id, 'user-id' => $el->id ])}}"><button class="btn btn-danger">Удалить</button></a></td>

i think this should work because it substitutes the data correctly and redirects to the page http://localhost:8000/org/11/delete/user/2. REturn 404 error.

It’s does not see the controller, I tried some function in the route itself, this does not work either.

2

Answers


  1. Route params should consist of alphabetic characters or _

    Please fix your route params name
    For example:

    Route::delete('/org/{org_id}/delete/user/{user_id}', 'AppHttpControllersOrganizationController@deleteUserFromOrg')
    ->name('delete-user-from-org');
    

    And your view blade

     <td><a href="{{route('delete-user-from-org', ['org_id'=>$data->id, 'user_id' => $el->id ])}}"><button class="btn btn-danger">Удалить</button></a></td>
    
    Login or Signup to reply.
  2. Route::delete('/org/{org-id}/delete/user/{user-id}', 'AppHttpControllersOrganizationController@deleteUserFromOrg')
    ->name('delete-user-from-org');
    

    look at the Route::delete() part. In order to invoke that route i.e. delete-user-from-org, you will require a DELETE request method (or create a form with a hidden input via @method('delete')

    When you create a link with <a></a>, the request will be a GET (by default unless manipulated by js) which is why you are getting a 404 Not Found Error. You have two solutions.

    First Solution (via form):

    <form action="{{route('delete-user-from-org',['org-id'=>$data->id, 'user-id' => $el->id ])}}" method="POST">
        @method('DELETE')
     
        ...
    </form>
    

    Second solution (via ajax):

    fetch("{{route('delete-user-from-org',['org-id'=>$data->id, 'user-id' => $el->id ])}}", {
      method: 'DELETE',
    })
    .then(res => res.text()) // or res.json()
    .then(res => console.log(res))
    

    Third Solution (change the method): Not recommended. Avoid it.

    Route::get('/org/{org-id}/delete/user/{user-id}', 'AppHttpControllersOrganizationController@deleteUserFromOrg')
    ->name('delete-user-from-org');
    

    from Route::delete() to Route::get()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search