在Laravel 5.2中使用多个表进行身份验证 [英] Authentication using multiple tables in Laravel 5.2

查看:132
本文介绍了在Laravel 5.2中使用多个表进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用5.2版创建laravel应用程序.哪里会有3种类型的用户

I am trying to create a laravel application with version 5.2. Where there will be 3 types of users

  1. 管理员(网站管理员)-使用默认的"users"表 这.
  2. 所有者(前端的网站列表创建者)-使用 表"owners".
  3. 客户(访问者或注册用户) 访问者)-为此使用表格"customers".
  1. Administrator (website manager) - using default "users" table for this.
  2. Owners (Website listing creator from frontend) - using a table "owners" for this.
  3. Customer (Visitors or registered visitors) - using a table "customers" for this.

现在我的问题是:

我想确保登录所有者将获得正确的身份验证,并重定向到他们自己的(其他默认身份验证路由)路由.

i want to make sure login Owners will get proper authentication and redirect to their own (other then default Auth route) route.

与客户相同,他们主要是通过网站前端登录,因此他们的路线与所有者和管理员不同.这些客户还将获得身份验证.

And same with customer, and they will be mainly login through frontend of the website, so their route will be different from owners and Administrator. And these customer will also get authentication.

我该如何处理?我曾经处理过单个表,但是作为Laravel的新手,我不确定如何使用多个表.

How can i manage that? I have worked around with single table, but being as a new person to Laravel i am not sure how i can achieve with multiple table.

我已经检查过laravel 5.2现在开始支持多个gaurds,但不确定如何做到这一点.

I have checked laravel 5.2 started supporting multiple gaurds now, but not sure how can i do this.

有一些用于此的程序包,但是我不想为此在程序包上进行中继.

There are certain packages for this, but i dont want to relay on package for this.

谢谢!

推荐答案

我建议您遵循 Polymorphic 方法.

假设有三个不同的表-administratorsownerscustomers

Let's say there are three different tables - administrators, owners, customers

现在所有这些都有一个名为users的公用表,该表将具有以下列:- profile_id profile_type .

Now for all of them, there is a common table with the name users which will have the columns :- profile_id, profile_type.

现在profile_id将成为表administratorsownerscustomers的外键,并且 profile_type 将告诉哪个 Model 用户所属.

Now profile_id will become the foreign key for tables administrators, owners and customers and profile_type will tell which Model the user belongs to.

关系就像

class User {

  public function profile() {
    return $this->morphTo();
  }

} 

--

class Administrator {

  public function user() {
    return $this->morphOne('App\User', 'profile');
  }

}

这里我们使用的是morphOne而不是morphMany,因为users表中的 profile_id 字段对于一位管理员来说应该只有一行.

Here we are using morphOne instead of morphMany because the profile_id field in users table should have only one row for one admin.

最后,出于创建/存储的目的.您必须:-

Lastly, for the purpose of creation/storing. You'll have to :-

  1. 创建

  1. Create an admin like

$admin = Administrator::create($inputs);

然后做

$user = new User($inputs);

$admin->user()->save($user);

您完成了!

您可以从 https://laravel.com了解有关此方法的更多信息/docs/5.1/eloquent-relationships#polymorphic-relations

谢谢

这篇关于在Laravel 5.2中使用多个表进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆