Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何动态更改用户表名?_Php_Laravel - Fatal编程技术网

Php 如何动态更改用户表名?

Php 如何动态更改用户表名?,php,laravel,Php,Laravel,我的应用程序有两类用户:教师和家长。登录时,必须指定您的类型 我在用户的构造函数中更改表名: public function __construct() { parent::__construct(); if(self::$userType === null) { self::$userType = request('user_type'); switch(self::$userTy

我的应用程序有两类用户:教师和家长。登录时,必须指定您的类型

我在用户的构造函数中更改表名:

public function __construct()
    {
        parent::__construct();

        if(self::$userType === null)
        {

            self::$userType = request('user_type');

              switch(self::$userType)
             {
                 case 'teacher':
                  self::$userType = 'teachers';
                  $this->table = self::$userType;
                 break;

                 default:
                   self::$userType = 'procreators';
                   $this->table = self::$userType;
                 break;

             }
        }

这个代码有效。但是,我决定在AuthenticatesUsers特性中更改失败的身份验证行为。就是这样:

$password = bcrypt($request->input('password'));
        $username = $request->input('name');

         if(!User::where('name', 'LIKE', $username)->exists())
         {
             throw ValidationException::withMessages([
                $this->username() => ['User login does not exist'],
             ]);
         }
         else if(!User::where('password', 'LIKE', $password)->exists())
         {
            throw ValidationException::withMessages([
                'password' => ['invalid password'],
             ]); 
         }

当我输入错误的密码时,会出现一个错误,显示“用户”表不存在。因此,这意味着它仍然使用旧名称“用户”。我不知道为什么。如何动态更改表的名称?显然,构造函数方法中的这段代码是不够的。

可能是静态的,而不是self?如果不是这样,请求('user_type')返回什么

static::$userType = 'teachers';

使用单独表格的理由是什么?通常,这是通过
角色
表(
学生
教师
角色)、
用户
上的
类型
列或
布尔值
来完成的,考虑到Laravel的AuthWork的工作方式,将用户划分为单独的表绝对是最困难的方法。教师和家长由不同的栏目描述。大多数老师的专栏对家长来说都是无用的。家长的专栏大多是供老师使用的。因此,将它们放在一个表中会创建冗余数据,可读性较差。如果列不同,则它们是单独的模型。切换表名将导致比您想象的更多的问题。您可能希望创建单独的模型,然后对其中任何一个进行身份验证。这可能对你有帮助;反省;在
用户
中保留相关数据(
电子邮件
密码
等),使用链接
用户id
将相关列添加到
教师
学生
表中,或者如果使用
角色
,则将数据作为
透视
列等。我同意您不需要多余的列,但是,还有一些替代解决方案可以保持单个
用户
表的完整性。(对不起,出于某种原因,我一直在读
学生
;同样的逻辑也适用于
家长
)你很可能说服了我。如果更改表的名称有很大问题,那么您的解决方案似乎是最好的。但我不明白为什么更改表名会有这么大的问题……我尝试了静态。这没有帮助。请求('user_type')返回用户在选择菜单中选择的正确值。我想在用户表中添加一个type列,而不是使用两个不同的表。否则,您可以尝试两种不同的模型,并根据用户登录的类型实例化正确的类。

Schema::rename('old_table_name', 'teacher');