1

How can I create multiple requests for the same route like below.

Route.php

Route::get('/home', 'HomeController@index');//->middleware('auth');
Route::get('/home/{$user}','HomeController@showStudent');
Route::delete('/home/{$studentId}','HomeController@deleteStudent');

the form was working fine until I have added the delete request. In my blade template I have code something like this.

home.blade.php

  <form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
                            {{ csrf_field() }}
                            <td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
                            </form>

I believe because of the same routes it's showing NotFoundHTTPException.

On one route /home I am trying to Add, Show, Edit and Delete a record with different buttons.

Thanks in Advance.

vsoni
  • 497
  • 1
  • 7
  • 22

3 Answers3

1

You could add a form and use Laravel's Form Method Spoofing

<input type="hidden" name="_method" value="DELETE">

See more here...http://laravel.com/docs/master/routing#form-method-spoofing

Try as below....

 <form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
 <input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">           
 <td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
 </form>
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0

1) Change you route from:

Route::delete('/home/{$studentId}','HomeController@deleteStudent');

To:

Route::get('/delete/{$Id}','HomeController@deleteStudent')->name('delete');

2) change you form tag from:

<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">

To:

<form class="" role="form" method="get" action="route('delete', ['id' => $student->id])">
vaibhav raychura
  • 162
  • 1
  • 10
-1

HTML forms doesn't support methods other than get and post. If you need to simulate it, include a hidden input to simulate delete:

<input name="_method" type="hidden" value="DELETE">

Then in your code, update it to:

<form class="" role="form" method="POST" action="/home/{{$student->id}}">
    {{ csrf_field() }}
    <input name="_method" type="hidden" value="DELETE">
    <td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>

Reference:

Community
  • 1
  • 1
Lionel Chan
  • 7,894
  • 5
  • 40
  • 69