我不明白 destroy 函数在这里是如何工作的?

I can't understand how the destroy function work here?

我正在处理旧的 laravel 项目,我必须修改现有项目。所以我现在正在尝试理解代码。该项目在 laravel 和 yajra 数据表上。我不明白 destroy 函数是如何工作的?在视图中没有调用 destroy 函数,但是当我单击删除按钮时它仍然有效。

控制器

public function loadSizes()
{
    $sizes = Size::select(['id', 'name', 'code'])->get();

    return DataTables::of($sizes)
        ->addColumn('action', function ($size) {
            return '<a href="' . url('/sizes/' . $size->id . '/edit') . '" class="btn btn-default edit_btn_styles" data-toggle="tooltip" data-placement="top" title="Update"><i class="fa fa-wrench" aria-hidden="true"></i></a>
                    <button type="button" data-id="' . $size->id . '" class="btn btn-default remove-size remove-btn" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fas fa-trash-alt" aria-hidden="true"></i></button>';
        })
        ->rawColumns(['action'])
        ->make(true);
}

public function destory(Request $request)
{
    $result = Size::where('id', $request->input('size_id'))->delete();
    if ($result) {
        return "SUCCESS";
    } else {
        return "FAIL";
    }

}

查看

@extends('layouts.sidebar')

@section('content')
<div class="row">
    <div class="col-sm-12 pad-main">
        <div class="row">
            <div class="col-md-6">
                <h4 class="cat-name"> Size List</h4>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12 table-responsive pad-tbl">
                <table class="table table-striped" id="size_table">
                    <thead>
                    <tr>
                        <th scope="col"> Name</th>
                        <th scope="col"> Code</th>
                        <th scope="col"> Action</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@if (Session::has('action'))
    @if (Session::get('action') == "create")
        @if (Session::has('status_success'))
                    <script > showAlert("SUCCESS", "Size creation successful");</script >
        @elseif(Session::has('status_error')))
            <script > showAlert("FAIL", "Size creation fail");</script >
        @endif
    @elseif(Session::get('action') == "update")
        @if (Session::has('status_success'))
            <script > showAlert("SUCCESS", "Size update successful");</script >
        @elseif(Session::has('status_error')))
            <script > showAlert("FAIL", "Size update fail");</script >
        @endif
    @endif
@endif    

<script>
    $(document).ready(function () {
        $('#size_table').DataTable({
            language: {
                searchPlaceholder: "Search records"
            },
            "columnDefs": [
                {"className": "dt-center", "targets": "_all"}
            ],
            processing: true,
            serverSide: true,
            ajax: '{!! url(' / load - sizes') !!}',
            columns: [
                {data: 'name', name: 'name'},
                {data: 'code', name: 'code'},
                {data: 'action', name: 'action'},
            ]
        });
    });

    $(document.body).on("click", ".remove-size", function () {

        var size_id = $(this).data('id');
        showConfirm("DELETE", "Do you want to delete this Size ?", "deleteSize(" + size_id + ")");
    });

    function deleteSize(id) {
        $.ajax({
            type: 'get',
            url: '{!! url('delete-size') !!}',
            data: {size_id: id},
            success: function (data) {
                if (data == "SUCCESS") {
                    $('[data-id="' + id + '"]').closest('tr').remove();
                    showAlert("SUCCESS", "Delete Size successful");
                }
            }, error: function (data) {

                showAlert("FAIL", "Delete Size fail");
            }
        });
    }
</script>
@endsection

在 blade 视图的底部,function destory(id) 中有一个 AJAX。 AJAX 正在向具有大小 ID 的 URL delete-size 发送 GET 请求。

现在,如果您在 web.php 文件中搜索 URL(位置 - routes/web.php),您会发现类似这样的内容:

Route::get('delete-size', 'SizeController@destory');

此路由会将尺码 ID 发送到您的 destory 函数,该函数会依次在您的数据库中搜索尺码并将其删除。

  //  Controller


 public function destroy($id)
    {
        Tag::find($id)->delete();
        Toastr::success('Tag Successfully Deleted',"Success");
        return redirect()->back();
    }
  //  Route

Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware'=>['auth','admin']], function (){

Route::resource('tag','TagController');

});

  //  HTML file

<form id="delete-form-{{ $tag->id }}" action="{{ route('admin.tag.destroy',$tag->id) }}" method="POST" style="display: none;">
    @csrf
     @method('DELETE')
     </form>