如何在不同的表中使用 Laravel 的 Auth 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32626339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use laravel's Auth class in different table?
提问by Jerielle
I created a simple login and registration in my page and I also added some new columns in the default users
table. Now my problem is I have another table named as admin
and the default Laravel's Auth table is users
. How can I implement the same functionality in my table?
我在我的页面中创建了一个简单的登录和注册,并且还在默认users
表中添加了一些新列。现在我的问题是我有另一个名为 as 的表admin
,默认 Laravel 的 Auth 表是users
. 如何在我的表中实现相同的功能?
In the Users model it has the declaration for the table name
在用户模型中,它具有表名的声明
protected $table = 'users';
Can you give me an example how can I use the default laravel's auth class?
你能给我举个例子,我怎样才能使用默认的 Laravel 的 auth 类?
Thats all thanks. :-)
就这些了,谢谢。:-)
回答by Jigs Virani
Do you hear about Multiauth in laravel. in this library there are two or more type user can login in one laravel application. In our case there are two type user Admin and Public that means User right.
你听说过laravel 中的 Multiauth 吗?在这个库中,有两种或多种类型的用户可以登录一个 Laravel 应用程序。在我们的例子中,有两种类型的用户 Admin 和 Public,这意味着用户权限。
Both forgot password and reset password functionality works separately in one application.
忘记密码和重置密码功能在一个应用程序中单独工作。
After install this library have have one step like below.
安装此库后,有如下步骤。
'multi' => [ 'admin' => [ 'driver' => 'database', 'table' => 'admin', 'email' => 'client.emails.password' ], 'users' => [ 'driver' => 'database', 'table' => 'users', 'email' => 'client.emails.password', ] ],
change your Auth.php file code with this one.
使用此更改您的 Auth.php 文件代码。
installation
安装
Firstly you want to include this package in your composer.json file.
首先,您希望将此包包含在您的 composer.json 文件中。
"require": {
"sboo/multiauth" : "4.0.*"
}
Now you'll want to update or install via composer.
现在您需要通过 composer 进行更新或安装。
composer update
Usage
用法
Everything is done the exact same way as the original library, the one exception being that all method calls are prefixed with the key (account or user in the above examples) as a method itself.
一切都以与原始库完全相同的方式完成,唯一的例外是所有方法调用都以键(上述示例中的帐户或用户)作为方法本身作为前缀。
Auth::admin()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::client()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::admin()->check();
Auth::client()->check();
回答by Bhaskar Bhatt
Laravel takes default users table for an application. For a change of laravel authentication different tablerelevant table name, we need to make a small change in authentication file of config.
Laravel 为应用程序采用默认的用户表。对于Laravel 认证不同表相关表名的改变,我们需要在config.js 的认证文件中做一个小改动。
Go to
去
config/auth.php
配置/auth.php
'providers' => [
// 'users' => [
// 'driver' => 'eloquent',
// 'model' => App\User::class,
// ],
'users' => [
'driver' => 'database',
'table' => 'user',
],
],
回答by Javier Nú?ez
I don't think the best way is to duplicate your table. I would extend users table with a role field that indicates if the user is a standard one or an admin. This way, you can keep the same code and add the ADMIN functionality that you are looking for.
我认为最好的方法不是复制您的表格。我将使用角色字段扩展用户表,该字段指示用户是标准用户还是管理员。这样,您可以保留相同的代码并添加您正在寻找的 ADMIN 功能。
If you NEED to do that and you are using Laravel 4, maybe you can use this plugin:
如果你需要这样做并且你正在使用 Laravel 4,也许你可以使用这个插件:
https://github.com/ollieread/multiauth/
https://github.com/ollieread/multiauth/
Also in this thread you have code that implements Auth in different tables:
同样在这个线程中,您有在不同表中实现 Auth 的代码:
https://gist.github.com/danielcoimbra/64b779b4d9e522bc3373
https://gist.github.com/danielcoimbra/64b779b4d9e522bc3373
But I strongly suggest to integrate both tables in one with an Admin flag/field
但我强烈建议将两个表与 Admin 标志/字段集成在一起