【问题标题】:Laravel 5.5 Check User exist with three param in two tablesLaravel 5.5检查用户是否存在两个表中的三个参数
【发布时间】:2019-02-08 17:27:56
【问题描述】:

我开发了注册模块,我想使用电子邮件、nationalCode 或 mobile 检查在我的 web 应用程序中注册的用户,我有两个表 users 和 userInfo ,我将电子邮件存储在 users 表中,并将 nationalCode 和 mobile 存储在 userInfo 表中,我想编写代码来检测我的两个表中是否存在用户的电子邮件或国家代码或手机,我显示用户已在我的站点中注册的警告文本,请帮助我完成这项工作,

我使用步骤表单并编写 ajax 来调用方法来完成此任务, 请注意,用户可能有三个匹配项,或者只有一个匹配项 感谢您的帮助:)

这里是ajax代码:

    $.ajax({
    url: url',
    type: 'POST',
    data: {
        _token: CSRF_TOKEN ,
        code:code,
        email:email,
        mobile:mobile,
    },
    dataType: 'JSON',
    success:function(data) {
        //return data
    }
});

这是我的方法是控制器

public function checkUser(Request $request)
{
    $email = $request->email;
    $mobile = $request->mobile;
    $code = $request->code;

    //here the query to detect user exist with three params
}

【问题讨论】:

    标签: php laravel laravel-5 registration


    【解决方案1】:

    假设您的关系定义如下:

    class User extends Model
    {
        public function info()
        {
            return $this->hasOne(UserInfo::class); 
        }
    }
    
    class UserInfo extends Model
    {
        public function user()
        {
            return $this->belongsTo(User::class); 
        }
    }
    

    ...然后你可以用这样的东西检查这个用户的存在。

    $user = User::where('email', $request->email)
        ->whereHas('info', function($query) use($request) {
            $query->where('mobile', $request->mobile)
                ->where('code', $request->code); 
        })
        ->exists();
    
    // user will be false if there's no record matching those parameters
    

    或者,如果您没有定义您的关系,那么您可能需要执行类似的操作。

    $user = User::where('email', $request->email)->exists();
    $info = UserInfo::where([
        'mobile' => $request->mobile,
        'code'   => $request->code
    ])->exists(); 
    
    if($user && $info) {
        // user exists 
    }
    

    我还是更愿意选择选项一:)

    【讨论】:

      【解决方案2】:

      如果你把唯一标识符放在你的表中,数据库会自动检测它并返回错误,但让数据库处理它不是一个好习惯,

      如果你想使用 Eloquent,那么查询应该是这样的

      public function checkUser(Request $request)
      {
          $email = $request->email;
          $mobile = $request->mobile;
          $code = $request->code;
          $user = User::query()->where('email', '=', $email)->orWhere('mobile','=',$mobile)
                  ->orWhere('code', '=',$code)->get();
          if($user) {
           // User already exits
             return;
          }
      }
      

      但是这个验证对我来说不好,更好的是使用 Laravel Requests https://laravel.com/docs/5.7/validation#form-request-validation

      要生成自定义请求,请使用此命令 (php artisan make:request RequestName)

      public function rules()
      {
          return [
              'title' => 'required|unique:users',
              'mobile' => 'required|unique:users',
              'code' => 'required|unique:users',
          ];
      }
      

      使用请求很简单

      public function checkUser(YourCustomRequest $request)
      {
          // Laravel will take care of all fields and check them if they exist in the database
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-19
        • 1970-01-01
        • 2018-06-01
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        • 2022-10-04
        • 1970-01-01
        • 2017-03-11
        相关资源
        最近更新 更多