gpt4 book ai didi

php - Laravel Auth::attempt 如何知道 Table

转载 作者:可可西里 更新时间:2023-11-01 07:02:36 27 4
gpt4 key购买 nike

我的数据库中有两个表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() }}

和授权 Controller

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');

}
}

最佳答案

Auth 使用您在文件 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;
}
}

现在在你的 Controller 中你可以这样做:

$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')))

...

关于php - Laravel Auth::attempt 如何知道 Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23431356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com