heizenberg
4 years ago

Laravel 5.1 and SweetAlert Ajax Token For Deleting a Product

Posted 4 years ago by heizenberg

Hello how can I get the token for deleting a product using SweetAlert and Ajax In Laravel 5.1

@foreach($products as $product)
    <tr>
        <td>{{ $product->name }}</td>
        <td>{{ $product->description }}</td>
        <td>{{ $product->updated_at->diffForHumans() }}</td>
        <td>{{ $product->created_at->diffForHumans() }}</td>
        <td>
            <div class="btn-group">
                {!! Form::open(['method' => 'DELETE', 'route' => ['dashboard.window-products.destroy', $product->id]]) !!}
                <a href="{{ route('dashboard.window-products.show', [$product->slug]) }}" class="btn btn-xs btn-default btn-flat"><i class="fa fa-eye"></i></a>
                <a href="{{ route('dashboard.window-products.edit', [$product->slug]) }}" class="btn btn-xs btn-default btn-flat"><i class="fa fa-edit"></i></a>

                <button type="button" class="deleteproductModal btn btn-xs btn-default btn-flat"
                        data-toggle="modal"
                        data-product_token="{{ csrf_token() }}"
                        data-product_name="{{ $product->name }}"
                        data-product_destroy_route="{{ route('dashboard.window-products.destroy', $product->id) }}">
                    <i class="fa fa-trash"></i>
                </button>

                {!! Form::close() !!}
            </div>
        </td>
    </tr>
@endforeach

<script>
    $('button.deleteproductModal').click(function()
    {
        var productRoute = $(this).attr("data-product_destroy_route");
        var productName = $(this).attr("data-product_name");
        var productToken = $(this).attr("data-product_token");

        deleteproduct(productRoute, productToken, productName);
    });

    function deleteproduct(productRoute, productToken, productName)
    {
        swal({
            title: "Window product Deletion",
            text: "Are you absolutely sure you want to delete " + productName + "? This action cannot be undone." +
            "This will permanently delete " + productName + ", and remove all collections and materials associations.",
            type: "warning",
            showCancelButton: true,
            closeOnConfirm: false,
            confirmButtonText: "Delete " + productName,
            confirmButtonColor: "#ec6c62"
        }, function()
        {
            $.ajax({
                type: "DELETE",
                url: productRoute,
                headers: { 'X-CSRF-TOKEN' : productToken }
            }).done(function(data)
            {
                swal("Window Product Deleted!", productName + " Window Product was successfully delete.", "success");
            }).error(function(data)
            {
                swal("Oops", "We couldn't connect to the server!", "error");
            });
        });
    }
</script>

Please sign in or create an account to participate in this conversation.