尝试查看用户是否存在时未定义的索引?

时间:2017-11-07 10:34:23

标签: php angularjs eloquent

每次输入关键字时,我都会得到一个未定义的索引,它会像这样显示在终端中。

基本上试图查看用户是否存在,然后在用户输入字段下输出。

我的功能有效,但我猜角度没有通过雄辩的数据库进行搜索?我无法完成这项工作。

提前致谢

enter image description here

enter image description here

(AuthController.php的一部分)

<?php


namespace App\Controllers\Auth;


use App\Controllers\BaseController;

use App\Auth\Auth;

use App\Models\User;




class AuthController extends BaseController
{

   public function check_user($request, $response)
   {
      $input = $request->getParsedBody();

      $user = new User();


      $myusername = $input['signupusername'];

      $username = filter_var($myusername, FILTER_SANITIZE_STRING);

      if(User::where('username', '=', $myusername)->exists()) {

            return "username already taken";
      }
   }

Main.js

angular.module('usernameScope.directive', [])
.directive('userExist', function ($http, $q){

   return {
        require: 'ngModel',
        link: function(scope, element, attrs, ngModel) {
            ngModel.$asyncValidators.userExist = function(modelValue, viewValue) {
                return $http.post('/auth/check_user', {username: viewValue})
                    .then(
                     function(response) {
                             if (!response.data.status) {
                                  return $q.reject(); 
                                 //Server will give me a  notify if it exist or not. I will throw a error If it exist with "reject"
                             }
                        return true;
                    }
                );
            };
        }
    };


});

1 个答案:

答案 0 :(得分:2)

我认为问题是由于这一行:

$input = $request->getParsedBody();

将此行更改为:

$input = $request->all();

或使用此

$myusername = $request->input('signupusername');