Reputation: 191
I want to create a dropdown with two links. A 'Delete' and a 'Edit' link.
For the delete function I created a form.
{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController@destroythread", $comment->id)) !!}
{!! Former::danger_submit('Delete') !!}
{!! Former::close() !!}
The form works, that means my comment get's deleted, if I'm pressing the button.
No I decided to remove the delete button and do a dropdown with a delete link. So I need to get the logic of this form in my dropdown menu.
But I haven't got this in the dropdown.. The optical 'Delete' button is this part of the dropdown:
<li><a href="#">
Delete
</a></li>
But I can't just put my controller function in that "href-link", cause without the 'DELETE'-Method, it won't work. I hope you all understand what I'm trying to say... my english isn't the best anyway.
Can somebody help me with this?
Thanks for any help!
I tried it like this before but this haven't worked either:
<li>
<a>
{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController@destroythread", $comment->id)) !!}
Delete
{!! Former::close() !!}
</a>
</li>
my try linking directly to the route:
<li><a href="{{ route('destroy', $comment->id) }}">Delete</a></li>
and my Route looks like this:
Route::delete('/show/{id}', 'Test\\TestController@destroythread')->name('destroythread');
but this haven't worked for me..
all /show/ routes:
Route::get('/show/{id}', 'Test\\TestController@show');
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController@update']);
Route::get('/show/{id}/edit', 'Test\\TestController@edit')->name('edit');
Route::delete('/show/{id}', 'Test\\TestController@destroy')->name('destroy');
Route::delete('/show/{id}', 'Test\\TestController@destroythread')->name('destroythread'); // this is the route we are talking about
Upvotes: 2
Views: 9567
Reputation: 26
Try this:
The form with method dont show and you can call a route / url with method POST/PUT/DELETE ...
<a href="{{ route('logout') }}" class="dropdown-item" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">Logout</a>
<form id="logout-form" action="{{ route('item/delete',) }}" method="POST" style="display: none;">
@csrf
</form>
Upvotes: 0
Reputation: 3042
Alternative way, try 'Laravel Collective' Html Helper.
HTML
{!! Form::open('delete',
'method' => 'delete,
'route' => ['show.destroy', $comment->id]
) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
routes.php
Route::delete('show/{show}', [
'uses' => 'TestController@destroy',
'as' => 'show.destroy'
]);
Upvotes: 0
Reputation: 7313
So, as per the discussion in the comments, you'll have to use ajax request to do a delete
request from an anchor tag.
$.ajax({
url: '/show/'+$('#testId').attr('value'),
type: 'DELETE',
success: function(data){ if(data.success) alert('Deleted'); },
error: function() {}
});
and in your route:
Route::delete('/show/{id}', ['as'=>'destroy', 'uses'=>'Test\\TestController@destroy']);
HTML
<li><a href="#" id="testId" value="{{$comment->id}}">Delete</a></li>
Upvotes: 0
Reputation:
Laravel uses method spoofing to do 'DELETE', 'PUT', 'PATCH' form requests. Like @Jilson Thomas mentioned, you can just create a link directly to the route. I suspect you're using resourceful routes, thats why you're trying to post a DELETE request?
Have a look at this section in the routing docs, this may help you out: https://laravel.com/docs/master/controllers#restful-supplementing-resource-controllers
Based on your routes posted, I believe the following two routes are matching before it gets to your desired route.
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController@update']);
Route::delete('/show/{id}', 'Test\\TestController@destroy')->name('destroy');
Try moving your desired route above these and see what happens.
Edit
<li><a href="{{ route('destroy', $comment->id) }}">Delete</a></li>
That will produce a GET request, therefore it will not match Route::delete(...). The previous method was posting a form to the route. Also, wrapping an entire form in an anchor tag is invalid markup.
Upvotes: 0