CRUD Laravel 4 how to link to destroy?

前端 未结 9 1404
你的背包
你的背包 2020-12-08 07:46

I will destroy my user with a HTML link, but it doesn\'t seem to generate the correct link, what am i doing wrong?

public function destroy($id)
{
    //Slet          


        
9条回答
  •  -上瘾入骨i
    2020-12-08 08:00

    An cool ajax solution that works is this:

    function deleteUser(id) {
        if (confirm('Delete this user?')) {
            $.ajax({
                type: "DELETE",
                url: 'users/' + id, //resource
                success: function(affectedRows) {
                    //if something was deleted, we redirect the user to the users page, and automatically the user that he deleted will disappear
                    if (affectedRows > 0) window.location = 'users';
                }
            });
        }
    }
    
    Delete
    

    And in the UserController.php we have this method:

    public function destroy($id)
    {
        $affectedRows  = User::where('id', '=', $id)->delete();
    
        return $affectedRows;
    }
    

     

提交回复
热议问题