Laravel 和 AngularJS 资源中的现有电子邮件验证

Laravel and AngularJS existing email verification within resource

我正在尝试使用 AngularJS 表单和 Laravel 作为后端创建一个新用户,该功能按预期工作,但检查具有现有电子邮件的用户除外。下面是我的 Angular FactoryController 函数 ngSubmitLaravel 控制器函数,用于处理表单输入。

UsersFactory.createNewUser = function(formData) {
    return $resource('api/users/create');
}


// controller function to process form    
$scope.processCreateUser = function() {
    // Add the user via Factory provided route and close the dialog
    UsersFactory.createNewUser().save($scope.formData).$promise.then(function(res) {
        $scope.formLoading = true;
        $timeout(function() {
            $scope.close();
        },1000);
    });
}
// Laravel Controller function
public function store(Request $request)
{
    $existingUser = User::where('email',$request->input('email'))->get();

    if(!empty($existingUser))
    {
        return response()->json(['error' => 'User Already Exist with the same email address.']);
    } else {
        // save user in model
    }
}

我似乎无法在前端显示错误消息,后端似乎工作正常。

解决了!

看来是我的 Laravel 控制器出了问题。更改为以下代码后,我的代码开始正常工作。

// controller function to process form    
$scope.processCreateUser = function() {
    // Add the user via Factory provided route and close the dialog
    UsersFactory.createNewUser().save($scope.formData).$promise.then(function(res) {
        if(res.error)
        {
          console.log("error exists");
        }
        $scope.formLoading = true;
        $timeout(function() {
            $scope.close();
        },1000);
    });
}

// Laravel controller function
public function store(Request $request)
{
    $existingUser = User::where('email',$request->input('email'))->get();

    if(count($existingUser) > 0)
    {
        return response()->json(['error' => 'User Already Exist with the same email address.']);
    } else {
        // save user in model
    }
}

我需要计算从 Laravel Eloquent 返回的集合数组,而不是用 !empty condition.Hope 检查,这对面临的人有帮助同样的情况。