如何在Laravel中创建两种类型的用户 [英] How to make two types of users in Laravel

查看:89
本文介绍了如何在Laravel中创建两种类型的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下如何在laravel中创建两种类型的用户.我有两个表,一个表给客户,一个表给客户,我的问题是如何发挥作用.我必须制作两个不同的模型还是使用模型User并在中间件中进行一些功能?

I want to ask how to make two types of users in laravel. I have two tables, one for the customer and one for the client and my question is how to make that difference. Do I have to make two different models or to use model User and make some functions in middleware?

谢谢.

推荐答案

如果您正在寻找最简单的解决方案,则可以将role列添加到users表中.然后,您可以通过以下方式全局检查用户是客户还是客户:

If you're looking for the simpliest solution, you can add role column to users table. Then you can check if user is a client or a customer globally with:

if (auth()->user()->role === 1) {
    // It's a client.
}

您可以添加一些帮助方法来检查用户是客户还是客户:

You can add some helper methods to check if user is a client or a customer:

public function isClient()
{
    return auth()->user() && auth()->user()->role === 1;
}

要只为客户打开网站的一部分,您应该使用路由组和中间件.

To open the part of the website for client only you should use route groups and middleware.

这篇关于如何在Laravel中创建两种类型的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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