Skip to content
Advertisement

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

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. 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).

Actually I’am using Breeze, however, I used Auth scaffolding (PHP artisan make: Auth) too

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:

JavaScript

and an extra function to override the default password of breeze or Auth (I guess):

JavaScript

next I went to conf/auth.php

there I specified the Model:

JavaScript

and the table to use:

JavaScript

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):

new name input:

JavaScript

After all this I went to LoginRequest (the validation for the login), where I replaced email for name_value

I tried to debug this:

JavaScript

and returns false

I noticed that there’s a function in vendor/laravel/ui/auth-back/AuthenticatesUsers called username(), that returns ’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

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)

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?)

Advertisement

Answer

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.

Just use the trait in your auth controller like this:

JavaScript

To override the password you can define this on your User model:

JavaScript

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement