简体   繁体   English

如何更改“用户”授权表并改用另一个 Laravel

[英]How to change 'users' auth table and use another instead Laravel

So I just started a Laravel Project with Breeze, and I wanted to change the default table users , the problem is it didn't work, I did my research for days and I didn't get any successful result所以我刚刚用 Breeze 启动了一个 Laravel 项目,我想更改默认表users ,问题是它没有用,我研究了几天但没有得到任何成功的结果

I will try to explain what is the problem and what have I tried so far.我将尝试解释问题所在以及到目前为止我尝试过的内容。

First, I created a new table called users_data , and this table, is completely different than the users table.首先,我创建了一个名为users_data的新表,这个表与users表完全不同。 The fields that users_data has, are for example: name_value , password_value , age_value , email_value , etc. (I have to mention too that for the table users_data , it doesn't use a migration, because I already have an sql file, and added it directly to the db (I already have tables created, with primary keys, and foreign key, so i couldn't do the migration because it would take me a lot of time), and without the migration I can still get the data, so I don't think it could be this the problem). users_data的字段,例如: name_valuepassword_valueage_valueemail_value等(我还得提一下,对于表users_data ,它不使用迁移,因为我已经有一个 sql 文件,并添加它直接到数据库(我已经创建了带有主键和外键的表,所以我无法进行迁移,因为这会花费我很多时间),如果没有迁移我仍然可以获得数据,所以我不认为这可能是这个问题)。

Actually I'am using Breeze, however, I used Auth scaffolding (PHP artisan make: Auth) too实际上我正在使用 Breeze,但是,我也使用了 Auth 脚手架(PHP artisan make:Auth)

What have I tried:我试过什么:

After several days of search, first I have created a new Model, called UsersModel , the content of this is the same as User Model however what I change is:经过几天的搜索,首先我创建了一个新的 Model,名为UsersModel ,其内容与User Model 相同,但我改变的是:

protected $table = 'users_data';

protected $fillable = [
    *name_value*,
    *password_value*,
];

and an extra function to override the default password of breeze or Auth (I guess):和一个额外的 function 来覆盖 breeze 或 Auth 的默认密码(我猜):

public function getAuthPassword()
{
    return $this->password_value;
}

next I went to conf/auth.php接下来我去了conf/auth.php

there I specified the Model:在那里我指定了 Model:

   'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\UsersModel::class,
        ],

and the table to use:和要使用的表:

/*
|--------------------------------------------------------------------------
| 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_data',

after this I went to the view login.blade.php , and changed only the email input (for what I read on different pages, changing the password input will cause different problems, because I would need to make a lot of changes to make it work so the best idea is to override it with getAuthPassword , specified in the model):在此之后,我转到视图login.blade.php ,并仅更改了email 输入(对于我在不同页面上阅读的内容,更改密码输入会导致不同的问题,因为我需要进行大量更改才能实现工作所以最好的想法是用模型中指定的getAuthPassword覆盖它):

new name input:新名称输入:

x-input id="email" class="block mt-1 w-full" type="text" name="name_value" :value="old('name_value')" required autofocus />

After all this I went to LoginRequest (the validation for the login), where I replaced email for name_value毕竟我去了LoginRequest (登录验证),我用 email 替换了name_value

I tried to debug this:我试着调试这个:

dd(Auth::attempt($this->only('name_value', 'password'), $this->boolean('remember')));

and returns false并返回假

I noticed that there's a function in vendor/laravel/ui/auth-back/AuthenticatesUsers called username() , that returns 'email'我注意到vendor/laravel/ui/auth-back/AuthenticatesUsers中有一个名为username()的 function,它返回“email”

when I saw that I remembered a page that said that this function could override too, so I changed the return value to name_value, and it doesn't do nothing当我看到的时候我记得有一个页面说这个function也可以覆盖,所以我把返回值改成name_value,并没有做任何事情

last, just to clarify,最后,澄清一下,

I don't need the Register site I only need the login page, so for that in the $fillable I didn't add all the columns of the database, just the ones that I need to log in ( name_value , password_value )我不需要注册站点我只需要登录页面,所以对于 $fillable 我没有添加数据库的所有列,只是我需要登录的那些( name_valuepassword_value

If anyone could help me and guide me it will be great, because I'am running out of ideas (I could do it with PHP alone, however, I need the ->middleware ['Auth] , is there a way to activate the middlware if the user exists?)如果有人可以帮助我并指导我,那就太好了,因为我的想法已经用完了(我可以单独使用 PHP 来完成,但是,我需要->middleware ['Auth] ,有没有办法激活中间件,如果用户存在?)

So you might have a model named users_data.php.所以你可能有一个名为 users_data.php 的 model。 Go inside it and change the code to something like that. Go 在里面并将代码更改为类似的东西。

STEP:1第1步

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\users_data as Authenticatable;  //Add this line for Auth.


class users_data extends Authenticatable   //Extends child from Authenticatable parent class obj.
{
    use HasFactory;
    protected $fillable = ['column1', 'column2', 'column3', .....];
}

STEP:2 Go to config/auth.php.步骤:2 Go 到 config/auth.php。 You may found something like this below.您可能会在下面找到类似的内容。

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

Change the 'model' => App\Models\User::class, to 'model' => App\Models\users_data::class, This is the main character in this drama that putting your application to users table by default.将 'model' => App\Models\User::class 更改为 'model' => App\Models\users_data::class,这是本剧的主角,默认将您的应用程序放入 users 表。

STEP:3 Comment off the User.php so that no future conflict create.步骤:3 注释掉 User.php,这样以后就不会产生冲突。 Now your application has been diverted to your desired table and ready to login.现在您的应用程序已转移到您想要的表并准备好登录。

I don't think it's good practice to be editing vendor files.我认为编辑供应商文件不是一个好习惯。 We don't push them to version control so other developers won't have your changes.我们不会将它们推送到版本控制,因此其他开发人员不会有您的更改。 Also, Laravel already has a way to override the username value without editing the vendor files.此外,Laravel 已经有一种方法可以在不编辑供应商文件的情况下覆盖用户名值。

Just use the trait in your auth controller like this:只需在您的身份验证控制器中使用该特征,如下所示:

public class MyLoginController {
   use AuthenticatesUsers; // or you can also use ThrottlesLogins trait

   // then override the username function here
   public function username() {
      return 'name_value';
   }
}

To override the password you can define this on your User model:要覆盖密码,您可以在User模型上定义:

public function getPasswordAttribute() {
  return $this->attributes['password_value'];
}

public function getAuthPassword() {
  return $this->password_value;
}

I haven't tested this but based on the docs this is how you should do it.我没有对此进行测试,但根据文档,您应该这样做。 Also make sure to read this Laravel doc .还要确保阅读这个Laravel 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM