2017-10-12 117 views
0

我有3種類型的用戶在如下應用:從其他表驗證在Laravel 5.4

1. Super Admin 
2. Normal User 
3. Staff 

我沒有設置爲超級管理員,但要設置登錄系統的「工作人員」用戶。我在Controllers文件夾/var/www/html/projecy_name/app/Http/Controllers創建一個文件夾名稱爲Staff並創建了一個控制器,名稱SessionsController.php,這裏是該文件中的內容:

<?php 

namespace App\Http\Controllers\Staff; 

use App\Http\Controllers\Controller; 
use Illuminate\Http\Request; 
use App\Staff; 

class SessionsController extends Controller 
{ 
    public function __construct() 
    { 
     $this->middleware('guest',['except'=>'destroy']); 
    } 

    public function create() 
    { 
     return view('staffsession.create'); 
    } 

    public function store() 
    { 
     echo request("email"); 
     $test = auth()->attempt(request(['email','password'])); 
     dd($test); 
     if(!auth()->attempt(request(['email','password']))) 
     { 
      Request()->session()->flash('error_msg', 'Please check your credentials and try again.'); 
      return back()->withErrors([ 
       'message' => 'Please check your credentials and try again.' 
      ]); 
     } 

     return redirect()->home(); 
    } 

    public function destroy() 
    { 
     auth()->logout(); 
     return redirect()->home(); 
    } 
} 
?> 

在存儲()函數,它在默認情況下將檢查admins 表,但我希望它必須檢查我的tbl_staff表。

如何做到這一點?

回答

1

您應該在\config\auth.php文件中定義一個新的警衛和提供者。

'guards' => [ 
    'web' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 

    'staff' => [ 
     'driver' => 'session', 
     'provider' => 'staff', 
    ], 

    'api' => [ 
     'driver' => 'token', 
     'provider' => 'users', 
    ], 
], 


'providers' => [ 
    'users' => [ 
     'driver' => 'eloquent', 
     'model' => App\User::class, 
    ], 

    'staff' => [ 
     'driver' => 'database', 
     'table' => 'tbl_staff', 
    ], 
], 

之後,您需要使用新的提供程序創建一個新的Guard實例。它將使用新表進行認證。

放置一個guard()方法到您的登錄控制器:

use Illuminate\Support\Facades\Auth; 

protected function guard() 
{ 
    return Auth::guard('staff'); 
} 

更新:

你必須以不同的方式使用你的方法,所以,它的作品。使用guard()方法代替auth()方法,因爲它使用默認防護。請參閱:

<?php 

namespace App\Http\Controllers\Staff; 

use App\Http\Controllers\Controller; 
use Illuminate\Http\Request; 
use App\Staff; 
use Illuminate\Support\Facades\Auth; 

class SessionsController extends Controller 
{ 
    public function __construct() 
    { 
     $this->middleware('guest',['except'=>'destroy']); 
    } 

    protected function guard() 
    { 
     return Auth::guard('staff'); 
    } 

    public function create() 
    { 
     return view('staffsession.create'); 
    } 

    public function store() 
    { 
     if(!$this->guard()->attempt(request(['email','password']))) 
     { 
      Request()->session()->flash('error_msg', 'Please check your credentials and try again.'); 
      return back()->withErrors([ 
       'message' => 'Please check your credentials and try again.' 
      ]); 
     } 

     return redirect()->home(); 
    } 

    public function destroy() 
    { 
     auth()->logout(); 
     return redirect()->home(); 
    } 
} 
+0

讓我檢查與解決方案 –

+0

它仍然是默認的表檢查 –

+0

@PathikVejani請檢查我的更新答案 –