Home > Mobile >  Route is not working, it responds with "404 NOT FOUND"
Route is not working, it responds with "404 NOT FOUND"

Time:01-23

I have a route which has two parameters.

Route::delete('/org/{org-id}/delete/user/{user-id}', 'App\Http\Controllers\OrganizationController@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 >Удалить</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.

CodePudding user response:

Route::delete('/org/{org-id}/delete/user/{user-id}', 'App\Http\Controllers\OrganizationController@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}', 'App\Http\Controllers\OrganizationController@deleteUserFromOrg')
->name('delete-user-from-org');

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

CodePudding user response:

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}', 'App\Http\Controllers\OrganizationController@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 >Удалить</button></a></td>
  • Related