Laravel 4 RESTfull deletion

the cleaner way


  1. Create a new js file in your public directory, 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();');
});

2. Don’t forget to include the .js file in your template after including jQuery.

3. 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']) }}

It seems much cleaner than bloating your code with Form::open(); in blade templates.

source: here

Email me when Kris Mulyandani P. publishes or recommends stories