1

I am trying to create a new user with AngularJS form and Laravel as backend, the functionality works as expected with exception to checking for users with existing email. Below is my Angular Factory, Controller function for ngSubmit and Laravel controller function for processing form inputs.

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
    }
}

I cannot seem to display the error message in the front-end, the backend seems to be working fine.

jrbedard
  • 3,662
  • 5
  • 30
  • 34
Rohan Krishna
  • 75
  • 2
  • 11

1 Answers1

0

Solved it!

It seems to be my Laravel Controller is messing up. After changing to code below, my code started working properly.

// 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
    }
}

I needed to count the collection arrays returned from Laravel Eloquent instead of checking with !empty condition.Hope this helps for someone facing the same situation.

Rohan Krishna
  • 75
  • 2
  • 11