CRUD Laravel 4 how to link to destroy?

前端 未结 9 1405
你的背包
你的背包 2020-12-08 07:46

I will destroy my user with a HTML link, but it doesn\'t seem to generate the correct link, what am i doing wrong?

public function destroy($id)
{
    //Slet          


        
相关标签:
9条回答
  • 2020-12-08 07:59

    This is an old questions, but I was just looking for a quick answer and am not satisfied with any of these. What I would suggest to anyone with this same problem is create a new route. Worrying too much about crud compliance is silly, because there is no such thing over HTML; any solution is just shoe-horned to fit, whether it's a hidden form field or a get route.

    So, in your routes, you likely have something like this:

    Route::resource('users', 'UsersController'); The problem with this is that the only way to get to the "destroy" method is to sent a post request which has a hidden input named "_method" and a value of "DELETE".

    Simply add under that line: Route::get('users/{id}/destroy',['as'=>'users.delete','uses'=>'UsersController@destroy']);

    Now you have a route you can access from HTML::linkRoute, Route::url, or whatever method you please.

    For example: {{ HTML::linkRoute( 'users.delete', 'Delete' , [ 'id' => $user->id ]) }}

    EDIT

    I want to clarify, though I have explained why it's somewhat silly to bend over backward to fit crud compliance, it is still true that your app will be more secure if changes are made only through POST requests.

    0 讨论(0)
  • 2020-12-08 07:59

    Want to send a DELETE request when outside of a form?

    Well, Jeffrey Way created a nice javascript that creates a form for you and to use it you only need to add data-method="delete" to your delete links.

    To use, import script, and create a link with the data-method="DELETE" attribute.

    script :

    (function() {
    
      var laravel = {
        initialize: function() {
          this.methodLinks = $('a[data-method]');
    
          this.registerEvents();
        },
    
        registerEvents: function() {
          this.methodLinks.on('click', this.handleMethod);
        },
    
        handleMethod: function(e) {
          var link = $(this);
          var httpMethod = link.data('method').toUpperCase();
          var form;
    
          // If the data-method attribute is not PUT or DELETE,
          // then we don't know what to do. Just ignore.
          if ( $.inArray(httpMethod, ['PUT', 'DELETE']) === - 1 ) {
            return;
          }
    
          // Allow user to optionally provide data-confirm="Are you sure?"
          if ( link.data('confirm') ) {
            if ( ! laravel.verifyConfirm(link) ) {
              return false;
            }
          }
    
          form = laravel.createForm(link);
          form.submit();
    
          e.preventDefault();
        },
    
        verifyConfirm: function(link) {
          return confirm(link.data('confirm'));
        },
    
        createForm: function(link) {
          var form = 
          $('<form>', {
            'method': 'POST',
            'action': link.attr('href')
          });
    
          var token = 
          $('<input>', {
            'type': 'hidden',
            'name': 'csrf_token',
              'value': '<?php echo csrf_token(); ?>' // hmmmm...
            });
    
          var hiddenInput =
          $('<input>', {
            'name': '_method',
            'type': 'hidden',
            'value': link.data('method')
          });
    
          return form.append(token, hiddenInput)
                     .appendTo('body');
        }
      };
    
      laravel.initialize();
    
    })();
    
    0 讨论(0)
  • 2020-12-08 08:00

    An cool ajax solution that works is this:

    function deleteUser(id) {
        if (confirm('Delete this user?')) {
            $.ajax({
                type: "DELETE",
                url: 'users/' + id, //resource
                success: function(affectedRows) {
                    //if something was deleted, we redirect the user to the users page, and automatically the user that he deleted will disappear
                    if (affectedRows > 0) window.location = 'users';
                }
            });
        }
    }
    
    <a href="javascript:deleteUser('{{ $user->id }}');">Delete</a>
    

    And in the UserController.php we have this method:

    public function destroy($id)
    {
        $affectedRows  = User::where('id', '=', $id)->delete();
    
        return $affectedRows;
    }
    

     

    0 讨论(0)
  • 2020-12-08 08:05

    For those looking to create delete button using anchor tag in laravel 5.

    {!! Form::open(['action' => ['UserController@destroy', $user->id], 'method' => 'DELETE', 'name' => 'post_' . md5($user->id . $user->created_at)]) !!}
        <a href="javascript:void(0)" title="delete" onclick="if (confirm('Are you sure?')) { document.post_<?= md5($user->id . $user->created_at) ?>.submit(); } event.returnValue = false; return false;">
            <span class="icon-remove"></span>
        </a>
    {!! Form::close() !!}
    
    0 讨论(0)
  • 2020-12-08 08:07

    I tried your code, used it like this and worked:

        <a href="{{URL::action('UserController@destroy',['id'=>$user->id]) }}" 
    onclick=" return confirm('Are you sure you want to delete this?')" 
    class="btn btn-default">
       DELETE     </a>
    

    I think that the only problem is in your array:

    ['id'=>$user->id]
    
    0 讨论(0)
  • 2020-12-08 08:13
    {{Form::open(['method'=>'delete','action'=>['ResourceController@destroy',$resource->id]])}}
     <button type="submit">Delete</button>                      
    {{Form::close()}}   
    
    0 讨论(0)
提交回复
热议问题