Laravel 5.1 and SweetAlert Ajax Token For Deleting a Product

Posted 3 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)
        <td>{{ $product->name }}</td>
        <td>{{ $product->description }}</td>
        <td>{{ $product->updated_at->diffForHumans() }}</td>
        <td>{{ $product->created_at->diffForHumans() }}</td>
            <div class="btn-group">
                {!! Form::open(['method' => 'DELETE', 'route' => ['dashboard.window-products.destroy', $product->id]]) !!}
                <a href="{{ route('', [$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-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>

                {!! Form::close() !!}

        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)
            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()
                type: "DELETE",
                url: productRoute,
                headers: { 'X-CSRF-TOKEN' : productToken }
                swal("Window Product Deleted!", productName + " Window Product was successfully delete.", "success");
                swal("Oops", "We couldn't connect to the server!", "error");

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

Reply to

Use Markdown with GitHub-flavored code blocks.