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="#">

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:

        {!! Former::horizontal_open()->method('DELETE')->action(action("Test\\[email protected]", $comment->id)) !!}
        {!! Former::close() !!}

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 



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:

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\\[email protected]']);
Route::delete('/show/{id}', 'Test\\[email protected]')->name('destroy');

Try moving your desired route above these and see what happens.


<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.




So, as per the discussion in the comments, you'll have to use ajax request to do adelete request from an anchor tag.

    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\\[email protected]']);


<li><a href="#" id="testId" value="{{$comment->id}}">Delete</a></li>



Alternative way, try 'Laravel Collective' Html Helper.


{!! Form::open('delete', 
    'method' => 'delete,
    'route'  => ['show.destroy', $comment->id]
) !!}

     {!! Form::submit('Submit') !!}

{!! Form::close() !!}


Route::delete('show/{show}', [
  'uses' => '[email protected]',
  'as' => 'show.destroy'



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;">

People are also looking for solutions to the problem: Deny all but localhost from one PHP file


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.