在Laravel 5.2中进行身份验证时如何使用电话号码而不是电子邮件?

I'm using Laravel 5.2 in a project and need the login form to use phonenumber instead of email for the credentials.

I do not want to edit any Illuminate package files.

You can manually authenticate the users:

public function authenticate(Request $request)
{
    $password=$request->password;
    $phone=$request->phone_number;
    if (Auth::attempt(['phone_number' => $phone, 'password' => $password]) )
    {     
        return redirect()->intended('/');   
    }
    else 
    {
        return redirect('/login');
    }

}

Use this while validating :

   $rules = array(
        'mobile'    => 'required', // make sure the mobile is an actual mobile
        'password' => 'required' // password can only be alphanumeric and has to be greater than 3 characters
    );


    // run the validation rules on the inputs from the form
    //$validator = Validator::make(Input::all(), $rules);

    $validator =  Validator::make(Input::all(), $rules);

    // if the validator fails, redirect back to the form
    if ($validator->fails()) {

        return Redirect::to('login')
            ->withErrors($validator) // send back all errors to the login form
            ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
    } else {

        //Create our user data for the authentication
        $userdata = array(
            'mobile'     => Input::get('mobile'),
            'password'  => Input::get('password')
        );

        $user = User::where('mobile',Input::get('mobile'))->with('userRoles')-   >first();

  if($user){
    if (Auth::attempt($userdata)) {

            /*if(User::isAdmin($user->id)) {
                return Redirect::to('/admin');
            }
            if(User::isAuthor($user->id)) {
                return Redirect::to('/dashboard');
            }*/
            // validation successful!
            // redirect them to the secure section or whatever
            // return Redirect::to('secure');
            // for now we'll just echo success (even though echoing in a controller is bad)
            //echo 'SUCCESSSSS!';
            return Redirect::to('/');

        } else {        
            // validation not successful, send back to form 
            return Redirect::to('login')->withErrors(['Invalid Username/Password']);

        }
  }

This should work:

// app/Http/Controllers/Auth/AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public $username = 'phonenumber';

    // ...

If you're using the standard 5.2 boilerplate for registration, you should also have to change the validator and create methods before they can work with your custom credentials.

Keep in mind this only works for 5.2. Laravel 5.3 changed many things in the Auth workflow.

in your auth controller add

public function loginUsername()
{
    return 'phonenumber';
}

make sure the name of your text input field is also called the same.