Skip to content

Instantly share code, notes, and snippets.

@parweb
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save parweb/d933efbf0d7334a79ef9 to your computer and use it in GitHub Desktop.

Select an option

Save parweb/d933efbf0d7334a79ef9 to your computer and use it in GitHub Desktop.
Tag <a> with any method type for laravel
/*
* Utilisation:
*
* add this in your body tag
* <body data-token="{{ csrf_token() }}">
*
* and enjoy ( that should change the world ^^ )
*
* <a method="delete" href="{{ route( 'users.destroy', $user_id ) }}">Delete a user</a>
* <a method="put" data-items='{ "title" : {{ $mynewtitle }} }' href="{{ route( 'post.update', $post_id ) }}">change the title</a>
*
* PUT, POST, GET, PATCH, HEAD supported, even plop works !
*/
$(document).ready( function () {
$('body').on( 'click', 'a[method]', function ( e ) {
e.preventDefault();
e.stopPropagation();
var $this = $(this);
var items = $(this).data('items') instanceof Object ? $(this).data('items') : {};
var $form = $('<form/>', {
id : 'fake_form',
method : 'post'.toUpperCase(),
action : $this.prop('href')
}).appendTo('body');
$method = $('<input/>', {
'type' : 'hidden',
'name' : '_method',
'value' : $this.attr('method'),
}).appendTo( $form );
$token = $('<input/>', {
'type' : 'hidden',
'name' : '_token',
'value' : $('body').data('token'),
}).appendTo( $form );
for ( var name in items ) {
var x = $('<input/>', {
'type' : 'hidden',
'name' : name,
'value' : items[name],
}).appendTo( $form );
}
$form.submit();
});
});
@parweb
Copy link
Copy Markdown
Author

parweb commented Aug 19, 2015

test giscus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment