I will destroy my user with a HTML link, but it doesn\'t seem to generate the correct link, what am i doing wrong?
public function destroy($id)
{
//Slet
For those looking to create the delete button in Laravel 5:
{!! Form::open(['action' => ['UserController@destroy', $user->id], 'method' => 'delete']) !!}
{!! Form::submit('Delete', ['class'=>'btn btn-danger btn-mini']) !!}
{!! Form::close() !!}
This is similar to Tayler's answer but we use the new blade escape tags ( {!!
and !!}
), we use the Form
facade to generate the button and we use a more elegant approach to link to the controller.
In Laravel < 5 the Forms & HTML package was pulled in automatically. In Laravel 5 we must add this package to composer.json
:
...
"required": {
...
"laravelcollective/html": "^5.1"
}
...
Now add the Service Provider and Alias in config/app.php
:
...
'providers' => [
...
Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
],
The above will output
<form method="POST" action="https://yourdomain.tld/users/1" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="xxxCrAZyT0K3nsTr!NGxxx">
<input class="btn btn-danger btn-mini" type="submit" value="Delete">
</form>
If you are using a different form builder just make sure it generates something similar to the above.
Update 08/21/2017 for Laravel 5.x
The question asks about Laravel 4, but I include this in case people looking for Laravel 5.x answers end up here. The Form helper (and some others) aren't available as of 5.x. You still need to specify a method on a form if you are doing something besides GET or POST. This is the current way to accomplish that:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<!-- other inputs... -->
</form>
You can also use {{ method_field('PUT') }}
instead of writing out the hidden _method
input.
See https://laravel.com/docs/5.4/routing#form-method-spoofing
Original Answer for Laravel 4
I think when you click the link, it is probably sending a GET request to that end point. CRUD in Laravel works according to REST. This means it is expecting a DELETE request instead of GET.
Here's one possibility from a tutorial by Boris Strahija.
{{ Form::open(array('route' => array('admin.pages.destroy', $page->id), 'method' => 'delete')) }}
<button type="submit" class="btn btn-danger btn-mini">Delete</button>
{{ Form::close() }}
This way, you send the request in a form with the DELETE method. The article explains why a traditional link won't work:
You may notice that the delete button is inside a form. The reason for this is that the destroy() method from our controller needs a DELETE request, and this can be done in this way. If the button was a simple link, the request would be sent via the GET method, and we wouldn’t call the destroy() method.
Another "clean" solution is to make it the Rails way as described here:
Create a new .js file in public and write this function:
$(function(){
$('[data-method]').append(function(){
return "\n"+
"<form action='"+$(this).attr('href')+"' method='POST' style='display:none'>\n"+
" <input type='hidden' name='_method' value='"+$(this).attr('data-method')+"'>\n"+
"</form>\n"
})
.removeAttr('href')
.attr('style','cursor:pointer;')
.attr('onclick','$(this).find("form").submit();');
});
Don't forget to include the .js file in your template after including jQuery.
Use classic link_to()
or link_to_method()
functions to create links to delete records. Just remember to include the "data-method"="DELETE"
parameter:
{{ link_to_route('tasks.destroy', 'D', $task->id, ['data-method'=>'delete']) }}
What I like about this that it seems much cleaner than bloating your code with Form::open();
in blade templates.