laravel method in href link?

后端 未结 3 717
萌比男神i
萌比男神i 2021-01-23 05:47

I want to create a dropdown with two links. A \'Delete\' and a \'Edit\' link.

For the delete function I created a form.

                        {!! Form         


        
相关标签:
3条回答
  • 2021-01-23 06:07

    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.

    0 讨论(0)
  • 2021-01-23 06:13

    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'
    ]);
    
    0 讨论(0)
  • 2021-01-23 06:18

    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>
    
    0 讨论(0)
提交回复
热议问题