I am following a tutorial about Laravel.
However, I want to convert the blade template Form::open() to html/php form, to make it easier to read & understand.
This is the Blade template:
{{ Form::open(['action'=> ['StudentController#destroy', $student->id], 'method'=>'POST']) }}
{{ method_field('DELETE') }}
{{ Form::submit('Delete',['class'=>'btn btn-danger']) }}
{{ Form::close() }}
I need to convert the blade code to html/php
I tried it multiple times, something like this. but failed.
<form action="url('StudentController#destroy', $student->id)" method="POST">
<?php method_field('Delete'); ?>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Anyone know the correct html/php form?
[edit] Route:list
try this way
use {{}} and use route
<form action="{{route('StudentController#destroy', ['id'=>$student->id])}}" method="POST">
<?php method_field('Delete'); ?>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
To call a controller action you need to use url()->action(...) (or action()) for short.
<form action="{{url()->action('StudentController#destroy', ['id'=>$student->id])}}" method="POST">
#csrf
{{ method_field('DELETE'); }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
This is also described in the manual
you should use this code
<form action="{{ url('StudentController#destroy', $student->id) }}" method="POST">
<input type='_method' value='DELETE' />
<button class="btn btn-danger" type="submit">Delete</button>
</form>
In your 'action' on the form, you need to enclose any helper functions within brackets so that Blade knows what to do with this, otherwise, it's just text.
Also note, I removed 'method_field' and replaced it with the hidden field, as this is essentially what method_field helper creates.
<form action="{{route('StudentController#destroy', ['id' => $student->id])}}" method="POST">
<input type='hidden' value='DELETE'>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
If using the route helper isn't working, you could use a more simple approach for the 'action' param of the form tag:
<form action="/student/destroy/{{$student->id}}" method="POST">
Related
I am trying to use Resource Controller and having a problem with destroying method, can't find a solution.
I get this error
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
web.php
Route::resource('honor', 'HonorController');
HonorController.php
public function destroy(Honor $honor)
{
dd($honor);
$honor->delete();
return redirect()->back();
}
blade
<form action="{{ route('honor.destroy', $honor->id) }}" method="post">
#csrf
#method('DELETE')
<div class="btn-group">
Edit
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</div>
</form>
Try changing the key (uses honor, not id) to match the name of the parameter in the route.
For example:
<form action="{{ route('honor.destroy', ['honor' => $honor->id]) }}" method="post">
#csrf
#method('DELETE')
<div class="btn-group">
Edit
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</div>
</form>
I'm working on a Laravel (v 5.4) project and i did the CRUD to manage categories. Currently, i can create a new category and i would be able to delete.
I created the view (with blade) to delete the categories :
<table class="table">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
#foreach ($categories as $category)
<tr>
<td>$category->name</td>
<td>
<a href="{{ url('/categories', ['id' => $category->id]) }}">
<button class="btn btn-default">
Delete
</button>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
And in the routing file web.php, i wrote :
Route::delete('/categories/{id}', CategoryController#destroy);
I have a controller CategoryController with a method destroy() who delete category and redirect to list of categories. But when i click on the button to delete, i get an error that explain this route is not define. If i replace Route::delete with Route::get it works. I think the url is called with GET but i would keep that for an other action.
I tried to replace the link with a form and "DELETE" as the value of "method" attribute but it didn't work.
How can i call url with DELETE method to catch it with Route::delete ?
Thanks in advance.
If you click on an url it will always be a GET method.
Since you wish to define it as DELETE, you should remake it into a post form and add
<input type="hidden" name="_method" value="delete" />
in it. Like replace:
<a href="{{ url('/categories', ['id' => $category->id]) }}">
<button class="btn btn-default">Delete</button>
</a>
with:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
<input type="hidden" name="_method" value="delete" />
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Same goes for PUT request.
Since Laravel 5.1 method_field:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
{!! method_field('delete') !!}
{!! csrf_field() !!}
</form>
Since Laravel 5.6 just with # tag:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
#method('delete')
#csrf
</form>
For laravel 5.7 please look my example:
<form action="{{route('statuses.destroy',[$order_status->id_order_status])}}" method="POST">
#method('DELETE')
#csrf
<button type="submit">Delete</button>
</form>
Any method other than GET and POST requires you to specify the method type using a hidden form input. That's how laravel detects them. In your case you need to send the delete action using a form. Do this.
<table class="table">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
#foreach ($categories as $category)
<tr>
<td>$category->name</td>
<td>
<form action="/categories/{{ $category->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-default" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
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.
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>
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])">
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:
Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
http://laraveldaily.com/theres-no-putpatchdelete-method-or-how-to-build-a-laravel-form-manually/
Here is my form :
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
<input type="hidden" name="_method" value="DELETE" />
</div>
</form>
Here is my controller :
public function destroy($id)
{
$invoice = Invoice::find($id);
if(!$invoice){
return redirect()->route('invoice.index')->with(['fail' => 'Page not found !']);
}
$invoice->delete();
return redirect()->route('invoice.index')->with(['success' => 'Invoice Deleted.']);
}
But it can not delete where is the problem ? How solve this ?
You need to use POST method for the form and add input element with name _method and value DELETE. Also, add token:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
I think you must add a hidden input to the form which will contain the method used:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input type="hidden" name="_method" value="DELETE" />
</form>
Read more on Laravel documentation about Form method spoofing
In order to get PUT and DELETE methods to work, you need an additional field, since only POST and GET are possible within HTML (out-of-the-box).
The additional field will be made with the code:
{!! method_field('DELETE') !!}
So your form will look like this:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
{!! method_field('DELETE') !!}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
Also, if you are using blade templates, you can add the method field like so:
#method('DELETE')
More Laravel way you can do this
<form action="{{ route('invoice.destroy',$invoice->id)}}" method="POST">
#method('DELETE')
<button type="submit" class="btn btn-primary">Yes</button>
</form>
I am using laravel 5.2 and I am unable to delete article in laravel. Below is my view link:
<form method="DELETE" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Below is my controller code:
public function destroy($id)
{
Article::destroy($id);
Session::flash('msg','Article deleted successfully');
return redirect()->back();
}
Below are route listing:
HTML forms don't actually support any methods other than GET and POST. To get around this Laravel spoofs the method and then picks this up in the request.
From the docs:
HTML forms do not support PUT, PATCH or DELETE actions. So, when
defining PUT, PATCH or DELETE routes that are called from an HTML
form, you will need to add a hidden _method field to the form. The
value sent with the _method field will be used as the HTTP request
method
As such, you just need to alter your form like so:
<form method="POST" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
You can also generate the _method with {{ method_field('DELETE') }} using Blade.
In your view file what you need to do is...
<form method="POST" action="/article/{{ $article->id }}">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>