Laravel Auth::attempt 如何知道 Table
Posted
技术标签:
【中文标题】Laravel Auth::attempt 如何知道 Table【英文标题】:Laravel How Auth::attempt knows about Table 【发布时间】:2014-06-19 07:52:03 【问题描述】:我的数据库中有两个表 1:物品 3:用户 并尝试登录用户并且它工作正常,但我的问题是我没有提到它将从哪个表中获取数据,但它会自动从所需的表中获取?它是 Laravel 的功能还是做错了什么? 或者我不明白为什么会这样?
表格
Form::open(array('class'=> 'forming'))
Form::text('username',null,array( 'placeholder' => 'Username' ,'class' => 'insi')); <br>
Form::password('password',array('placeholder'=>'Password', 'class'=>'paswor')); <br>
Form::submit('SignIn!',array('class'=> 'but-n'));
Form::close()
和 AuthController
class AuthController extends Controller
public function getLogin()
return View::make('login');
public function postLogin()
$rules = array('username'=> 'required', 'password'=>'required');
$validator = Validator::make(Input::all(),$rules);
if($validator->fails())
return Redirect::route('login')
->withErrors($validator);
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
), false);
if(!$auth)
return Redirect::route('login')
->withErrors(array(
'Invalid'
));
return Redirect::route('home');
【问题讨论】:
【参考方案1】:Auth 使用您在 file app/config/auth.php
中的模型:
如果您使用 Eloquent 驱动程序 进行身份验证:
/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => 'User',
如果您使用的是数据库驱动程序:
/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'table' => 'users',
如果您需要使用多个表进行身份验证,您有一些选择,其中之一是使用这样的结构进行登录:
class Logon
public function loginViaEmail($email, $password)
if ($emailModel = Email::where('email', $email)->first())
return $this->login($emailModel->user_id, $password);
return false;
public function loginViaName($name, $password)
if ($memberModel = Member::where('name', $name)->first())
return $this->login($memberModel->user_id, $password);
return false;
public function loginViaId($id, $password)
if ($beingModel = Being::where('id', $id)->first())
return $this->login($beingModel->user_id, $password);
return false;
public function login($id, $password)
$user = Member::find($id);
if(Hash::check($password, $user->password))
Auth::loginUsingId($id);
return true;
return false;
现在您可以在控制器中执行此操作:
$logon = new Logon;
if ( ! $logon->loginViaEmail(Input::get('email'), Input::get('password')))
return "username or password invalid";
return "logged in successfully";
或
...
if ( ! $logon->loginViaName(Input::get('name'), Input::get('password')))
...
【讨论】:
如果您使用的是数据库驱动程序而不是 Eloquent 驱动程序,那么下面就是要使用的表格。 好的,那么 Eloquent 和 Auth 是否具有相同的功能? Auth 可能会使用 Eloquent 访问您的用户表并对其进行身份验证,但它们没有相同的功能,它们是完全不同的东西。 您对 Auth 访问数据库感到困惑。您必须了解 Laravel 由许多不同的服务组成,Auth 是一种服务,Database 是另一种服务。 Auth 服务可能使用 Database 服务(Eloquent 或 Database,两者都是数据库服务),Session 服务也可能使用 Database 服务,但这些东西并没有真正的关系,它们只是相互使用,你可以告诉它们使用不同的服务(交换服务)。假设数据库对于您的 Cache 服务来说太慢了,您可以告诉 Cache 使用不同的服务:memcached。 查看文档以尝试了解更多信息:laravel.com/docs、laravel.com/docs/eloquent、laravel.com/docs/security。以上是关于Laravel Auth::attempt 如何知道 Table的主要内容,如果未能解决你的问题,请参考以下文章
在 Auth::attempt() Laravel 5.1 中禁用重定向
Laravel Auth::attempt() 可以处理关系吗?
Laravel 5 Auth::attempt() 总是返回 false