Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过Laravel Auth()中的电子邮件/电话号码登录_Php_Laravel_Authentication - Fatal编程技术网

Php 通过Laravel Auth()中的电子邮件/电话号码登录

Php 通过Laravel Auth()中的电子邮件/电话号码登录,php,laravel,authentication,Php,Laravel,Authentication,我正在使用 Route::auth(); 用于在Laravel中进行用户登录。 有多个电话链接到一个用户并保存在表:电话中。 桌子是 用户:id、电子邮件、密码 电话:id、用户id、电话号码 如何让用户同时使用电子邮件/电话和密码登录在App\Traits\Auth中,创建一个名为logiuser.php的文件 <?php namespace App\Traits\Auth; use App\User; use Illuminate\Http\Request; use Illumi

我正在使用

Route::auth();
用于在Laravel中进行用户登录。 有多个电话链接到一个用户并保存在表:电话中。 桌子是 用户:id、电子邮件、密码 电话:id、用户id、电话号码


如何让用户同时使用电子邮件/电话和密码登录在
App\Traits\Auth
中,创建一个名为
logiuser.php
的文件

<?php

namespace App\Traits\Auth;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

trait LoginUser
{

    /**
     * Handle a Authenticates the User.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function login(Request $request)
    {
        $this->validateLogin($request);

        if ($this->attemptLogin($request)) {
            return $this->successfulLogin($request);
        }
        return $this->failedLogin($request);
    }

    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required',
        ]);
    }
    /**
     * Attempt to log the user into the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function attemptLogin(Request $request)
    {   
        //Try with email AND username fields
        if (Auth::attempt([
            'phone' => $request['username'],
            'password' => $request['password']
            ],$request->has('remember'))
            || Auth::attempt([
            'email' => $request['username'],
            'password' => $request['password']
            ],$request->has('remember'))){
                return true;
        }
        return false;
    }

    /**
     * This is executed when the user successfully logs in
     * 
     * @var Request $request
     * @return Reponse
     */
    protected function successfulLogin(Request $request){
        return redirect($this->redirectTo);
    }

    /**
     * This is executed when the user fails to log in
     * 
     * @var Request $request
     * @return Reponse
     */
    protected function failedLogin(Request $request){
        return redirect()->back()->withErrors(['password' => 'You entered the wrong username or password']);
    }

}
最后,在路由文件中,添加以下路由:

Route::get('login', 'Auth\LoginController@show');
Route::post('login', 'Auth\LoginController@login');

您可以验证数据库中的详细信息,然后可以使用Auth::loginUsingId($user\u id);登录用户谢谢@geekbro。尝试此操作您是否尝试过使用
Auth::basic('username')
?首先我正在签入数据库,如果用户凭据正确,则使用Auth::loginUsingId($user\u id);然后重定向到仪表板
Route::get('login', 'Auth\LoginController@show');
Route::post('login', 'Auth\LoginController@login');