php - laravel method in href link?
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\\[email protected]", $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\\[email protected]", $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\\[email protected]')->name('destroythread');
but this haven't worked for me..
all /show/ routes:
Route::get('/show/{id}', 'Test\\[email protected]');
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\[email protected]']);
Route::get('/show/{id}/edit', 'Test\\[email protected]')->name('edit');
Route::delete('/show/{id}', 'Test\\[email protected]')->name('destroy');
Route::delete('/show/{id}', 'Test\\[email protected]')->name('destroythread'); // this is the route we are talking about
Answer
Solution:
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.
Try moving your desired route above these and see what happens.
Edit
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.
Answer
Solution:
So, as per the discussion in the comments, you'll have to use ajax request to do a
delete
request from an anchor tag.and in your route:
HTML
Answer
Solution:
Alternative way, try 'Laravel Collective' Html Helper.
HTML
routes.php
Answer
Solution:
Try this:
The form with method dont show and you can call a route / url with method POST/PUT/DELETE ...