laravel通过make auth实现手机号登录

首先按照Laravel的教程,安装认证系统.

php artisan make:authphp artisan migrate

laravel已经安装完成认证系统,默认注册和登录都是用邮箱.

如果想成用手机号注册登录,需要进行如下的修改

1.在数据库users表中,增加phone字段

2.修改users表中的name和email字段

ALTER TABLE `users` CHANGE `name` `name` VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, CHANGE `email` `email` VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;

  

3.修改文件RegisterController中的create方法

/*** Create a new user instance after a valid registration.** @param  array  $data* @return \App\User*/protected function create(array $data){return User::create([//'name' => $data['name'],

        'phone' => $data['phone'],

            //'email' => $data['email'],'password' => Hash::make($data['password']),]);}

4.修改文件RegisterController中的validator方法

/*** Get a validator for an incoming registration request.** @param  array  $data* @return \Illuminate\Contracts\Validation\Validator*/protected function validator(array $data){return Validator::make($data, [// 'name' => ['required', 'string', 'max:255'],

        //长度11 在users中不能重复
        phone' => ['required', 'string','size:11','unique:users'],

           // 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],'password' => ['required', 'string', 'min:6', 'confirmed'],]);}

5.修改User.php模型文件,添加phone属性

protected $fillable = ['phone','name', 'email', 'password'];

  

6.修改登录控制器LoginController,添加username方法,返回phone字段名,作用是在登录时以phone做为判断

      public function __construct(){$this->middleware('guest')->except('logout');}public function username(){return 'phone';}

  

7.修改视图文件auth/register.blade.php,增加手机号输入项

@csrf@if ($errors->has('phone')){{ $errors->first('phone') }}@endif@if ($errors->has('password')){{ $errors->first('password') }}@endif

 

8.修改登录视图login.blade.php

@csrf@if ($errors->has('phone')){{ $errors->first('phone') }}@endif@if ($errors->has('password')){{ $errors->first('password') }}@endif{ old('remember') ? 'checked' : '' }}>{{ __('Forgot Your Password?') }}

  

好,现在注册与登录都使用手机号phone

转载于:https://www.cnblogs.com/cutchaos/p/9836177.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部