CRUD Laravel 5 how to link to destroy of Resource Controller?

后端 未结 10 2175
迷失自我
迷失自我 2020-12-15 17:27

I have a link



        
相关标签:
10条回答
  • 2020-12-15 18:14

    This is because you are requesting the resources via GET method instead DELETE method. Look:

    DELETE  /photo/{photo}  destroy     photo.destroy
    GET     /photo/{photo}  show    photo.show
    

    Both routes have the same URL, but the header verb identifies which to call. Looks the RESTful table. For example, via ajax you can send a DELETE request:

    $.ajax({
        url: '/user/4',
        type: 'DELETE',  // user.destroy
        success: function(result) {
            // Do something with the result
        }
    });
    
    0 讨论(0)
  • 2020-12-15 18:16

    I use this template 'resources/views/utils/delete.blade.php'

    <form action="{{ $url or Request::url() }}" method="POST">
        {{ method_field('DELETE') }}
        {{ csrf_field() }}
        <button type='submit' class="{{ $class or 'btn btn-danger' }}" value="{{ $value or 'delete' }}">{!! $text or 'delete' !!}</button>
    </form>
    

    Called as this:

    @include('utils.delete',array( 'url' => URL::route('user.destroy',$id),'text' => '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> delete me'))
    
    0 讨论(0)
  • 2020-12-15 18:16

    In case someone came here to find how to replace standard laravel form for delete, from button in it to link, you can just replace:

    {!! Form::open(['method' => 'DELETE', 'route' => ['tasks.destroy', $task->id],'onsubmit' => 'return confirm("Are you sure?")', 'id'=>'himan']) !!}
    
        {!! Form::submit('Delete') !!}
    
    {!! Form::close() !!}
    

    TO

    {!! Form::open(['method' => 'DELETE', 'route' => ['tasks.destroy', $task->id],'onsubmit' => 'return confirm("Are you sure?")', 'id'=>'himan']) !!}
    
        <a href="#" onclick="$(this).closest('form').submit();">Delete</a>
    
    {!! Form::close() !!}
    

    Just replace button with simple <a href="#"... but with onclick attribute to submit the form!

    0 讨论(0)
  • 2020-12-15 18:19

    GET and DELETE Both routes have the same URL, but the header verb identifies which to call.

    Here are my code snippets for edit and delete. I use bootstrap modal confirmation for delete action

    <div class="btn-group">
      <a href="{{ route('locations.edit', $location->id) }}"
       class="btn btn-default btn-sm">
        <i class="fa fa-pencil"></i>
      </a>
      <span class="btn btn-danger btn-sm formConfirm"
          data-form="#frmDelete-{{$location->id}}"
          data-title="Delete Location"
          data-message="Are you sure you want to delete this Location ?">
          <i class="fa fa-times"></i>
      </span>
    <form method="POST"
          style="display: none"
          id="frmDelete-{{$location->id}}"
          action="{{ route('locations.destroy' , $location->id) }}">
        {!! csrf_field() !!}
        {{ method_field('DELETE') }}
        <input type="submit">
    </form>
    

    BootStrap Modal

    <div class="modal fade" id="formConfirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                            class="sr-only">Close</span></button>
                <h4 class="modal-title" id="frm_title">Delete</h4>
            </div>
            <div class="modal-body" id="frm_body"></div>
            <div class="modal-footer">
                <button style='margin-left:10px;' type="button" class="btn btn-primary col-sm-2 pull-right"
                        id="frm_submit">Yes
                </button>
                <button type="button" class="btn btn-danger col-sm-2 pull-right" data-dismiss="modal" id="frm_cancel">
                    No
                </button>
            </div>
        </div>
    </div>
    

    And Finally JS code

    $('.formConfirm').on('click', function (e) {
      e.preventDefault();
      var el = $(this);
      var title = el.attr('data-title');
      var msg = el.attr('data-message');
      var dataForm = el.attr('data-form');
    
      $('#formConfirm')
        .find('#frm_body').html(msg)
        .end().find('#frm_title').html(title)
        .end().modal('show');
    
      $('#formConfirm').find('#frm_submit').attr('data-form', dataForm);
    });
    
    $('#formConfirm').on('click', '#frm_submit', function (e) {
      var id = $(this).attr('data-form');
      $(id).submit();
    });
    
    0 讨论(0)
提交回复
热议问题