来自不同表的Laravel身份验证不起作用



在我的laravel项目中,我想为代理实现登录功能。我正在尝试让代理从'agents'表登录。我已经创建了控制器,并在config/auth.php中添加了控制器,但它不起作用。

以下是我在模型中的代码

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use IlluminateNotificationsNotifiable;
use KyslikColumnSortableSortable;
use IlluminateFoundationAuthUser as Authenticatable;
class Agencie extends Authenticatable
{
use Notifiable;
use Sortable;
protected $table = 'agencies';
protected $primaryKey = 'agency_id';    
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'agency_id', 'agency_name','agency_user_name' ,'agency_password', 'agency_city', 'agency_state', 'agency_zip', 'agency_email','status','created_at','updated_at'
];
}

以下几行我已添加到providers & guards下的config/auth.php中

'guards' => [
'agency' => [
'driver' => 'session',
'provider' => 'agency',
],
],
'providers' => [
'agency' => [
'driver' => 'eloquent',
'model' => AppAgencie::class,
],
],

以下是routes.php 中的代码

Route::group(['prefix' => 'agency'], function () {
Route::get('/', 'AgencyAgencyAuthLoginController@showLoginForm')->name('agency_login');
Route::get('/login', 'AgencyAgencyAuthLoginController@showLoginForm')->name('login');
Route::post('/login', 'AgencyAgencyAuthLoginController@login');
});

以下是控制器中的代码

<?php
namespace AppHttpControllersAgencyAgencyAuth;
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateSupportFacadesAuth;
use HestoMultiAuthTraitsLogsoutGuard;
use JsValidator;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers, LogsoutGuard {
LogsoutGuard::logout insteadof AuthenticatesUsers;
}

protected $validationRules = [
'email' => 'required|email',
'password' => 'required'
];
/**
* Where to redirect users after login / registration.
*
* @var string
*/
// public $redirectTo = '/user/home';
public $redirectTo = '/user/dashboard-graph';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('admin.guest', ['except' => 'logout']);
}
/**
* Show the application's login form.
*
* @return IlluminateHttpResponse
*/
public function showLoginForm()
{
$validator = JsValidator::make($this->validationRules,[],[],'#loginform');
return view('agency.auth.login')->with('validator', $validator);
}
/**
* Get the guard to be used during authentication.
*
* @return IlluminateContractsAuthStatefulGuard
*/
protected function guard()
{
return Auth::guard('agency');
}
/**
* Get the needed authorization credentials from the request.
*
* @param  IlluminateHttpRequest  $request
* @return array
*/
protected function credentials(IlluminateHttpRequest $request)
{
//return $request->only($this->username(), 'password');
return ['email' => $request->{$this->username()}, 'password' => $request->password, 'status' => 'Active'];
}
}

我还没有添加查看页面的代码,登录表单加载成功,但如果我从agency_table提供"电子邮件id"one_answers"密码",它就不会登录。

显示以下错误

Column not found: 1054 Unknown column 'email' in 'where clause

我的表名是admin_email请帮忙,这里有什么问题

表中确实有电子邮件字段吗?默认情况下,Laravel使用电子邮件作为用户名作为身份验证数据。

正如你的桌子上有";agency_email";列,因此您可能不得不更改您的登录控制器,因为文档显示

用户名自定义

默认情况下,Laravel使用电子邮件字段进行身份验证。如果你想自定义,你可以在LoginController上定义一个用户名方法:

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

最新更新