我尝试使用Ajax删除数据但无法正常工作

时间:2020-03-28 06:00:31

标签: javascript ajax laravel

当我尝试使用Ajax删除ID时,它不起作用。 我已经尝试了使用路由名称和路由的多种方法。 但这行不通。

我的查看页面代码是:

<a href="javascript:void(0)" id="delete-product" data-id="{{ $product->id }}" class="btn btn-danger delete-product">Delete</a>

我的控制器代码是:

function removedata(Request $request)
{
    $product= Product::find($request->input('id'));
    if($product->delete())
    {
        echo 'Data Deleted';
    }
}

我的路线是:

Route::get('/product/remove', 'Product\ProductController@removedata')->name('product.remove');

我的脚本是:

$(document).ready(function () {   
    $('body').on('click', '.delete-product', function () {
        var user_id = $(this).data("id");   

        if(confirm("Are you sure you want to Delete this data?"))
        {
            $.ajax({
                url:"{{route('product.remove')}}",
                mehtod:"get",
                data:user_id,
                success:function(data)
                {
                    alert("sucess");
                }
            })
        }
        else
        {
            return false;
        }
    });
});

2 个答案:

答案 0 :(得分:0)

$product= Product::find($request->input('id'));
dd($product);--------------------------->(What is the output here)
if($product->delete())
{
    echo 'Data Deleted';
}

答案 1 :(得分:0)

选中此项并更新您的代码:

class ViewController: UIViewController {

    @IBOutlet weak var fish: UIImageView!
    @IBOutlet weak var fishLeading: NSLayoutConstraint!

    enum Direction: CGFloat {
        case left = -1
        case right = 1

        func reversed() -> Direction {
            return Direction(rawValue: -rawValue)!
        }
    }

    var fishAnimator: UIDynamicAnimator?

    var fishDirection = Direction.right

    @objc func onFishTap(_ sender: UITapGestureRecognizer) {
        let animator = UIDynamicAnimator(referenceView: water)

        let collisionBehavior = UICollisionBehavior(items: [fish])
        collisionBehavior.translatesReferenceBoundsIntoBoundary = true
        collisionBehavior.collisionDelegate = self
        collisionBehavior.collisionMode = .boundaries
        animator.addBehavior(collisionBehavior)

        let pushBehavior = UIPushBehavior(items: [fish], mode: .instantaneous)
        pushBehavior.magnitude = 0.0000001
        pushBehavior.pushDirection = .init(dx: fishDirection.rawValue, dy: 0)
        animator.addBehavior(pushBehavior)

        self.fishAnimator = animator
    }
}

extension ViewController: UICollisionBehaviorDelegate {
    func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint) {

        fishDirection = fishDirection.reversed()
        UIView.animate(withDuration: 0.3, animations: {
            self.fish.transform = .init(scaleX: self.fishDirection.rawValue, y: 1)
        })
        fishLeading.constant = fish.center.x - fish.frame.size.width / 2 + fishDirection.rawValue * 2
        fishAnimator?.removeAllBehaviors()
        fishAnimator = nil
    }
}