简体   繁体   中英

How to use more than one table to auth in Laravel 5.6 ?

This is my tables structure i want to make auth in laravel 5.6 :

permission : permission_id , permission

login : login_id , permission_id , Full_name , password

email : email_id , login_id , email

phone : phone_id , login_id , phone_number

users can login with email and password or phone with password each user can have many phone and email and I have 3 type of permission user , admin , manager.

how can I do that ? this is my code it work if email and phone was the field in login table but in my case email and phone are separated from login table because each user can have more than one email or phone.

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller {

use AuthenticatesUsers;

    public function showLoginForm() {
        return view('panel.login');
    }

     public function username()
    {
        return 'email';
    }


    protected function attemptLogin(Request $request)
    {

        $username = $request->username;

        return $this->guard()->attempt(
            $this->credentials($request), $request->filled('remember')
        );

    }

    protected $redirectTo = '/panel';

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

}
?>

You can write this. Hopefully this will solve your problem.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller {
    use AuthenticatesUsers;
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/panel';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct() {
        $this->middleware('guest', ['except' => 'logout']);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login() {
        return view('panel.login');
    }
    protected function credentials(Request $request) {
        $field = filter_var($request->input($this->username()), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
        $request->merge([$field => $request->input($this->username())]);
        return array_merge($request->only($field, 'password'));
    }
    public function username() {
        return 'email';
    }
    public function authenticate(Request $request) {
        $credentials = $this->credentials($request);
        if (Auth::attempt($credentials)) {
            return redirect()->intended('/');
        } else {
            return redirect()->intended('login')->with('error', 'Invalid login credentials. Check your email address or username and password!');
        }
    }
}

thanks for all for replays
i solve it and this is my code what id did is i make login with login_id and password but i get login_id from email or phone table so i get the login_id of email that user entered

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Email;
use App\Phone;

class LoginController extends Controller {


use AuthenticatesUsers;

    public function showLoginForm() {
        return view('panel.login');
    }

    public function username() {
        return 'username';
    }

    protected function attemptLogin(Request $request) {

        $id = '0';
        $username=trim( $request->username );
        if( filter_var($request->input($this->username()), FILTER_VALIDATE_EMAIL) ) {
           $id= Email::select('login_id')->where('email',$username)->first();

        }elseif (is_numeric($username)) {
            $id= Phone::select('login_id')->where('phone_number',$username)->first();
        }        
        $id=($id==null)?0:$id->login_id;        
        return $this->guard()->attempt(
                        ['Login_id' => $id, 'password' => $request->password], $request->filled('remember')
        );

    }


    protected $redirectTo = '/panel';

    public function __construct() {

        $this->middleware('guest')->except('logout');
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM