Database considerations
If you want to use your custom database on your laravel application, you just have to make a Model for each of the tables of your database and set up the table name, primary key and fillable attributes.
More info here Defining Models
Authentication with custom user and pass fields
If you executed the command `php artisan make:auth` but you want to use another table or fields instead of the default user and pass without breaking the created Auth functionality, you have configure Laravel for it, following the steps bellow:
First: Create a Model for the table that you want to use for authentication. To do it, run the command php artisan make:model FooUserTable, where 'FooUser' is the name of your table.
Next you have to set on the model your table name, primary key and fillable fields, like bellow:
class FooUser extends Authenticatable{
protected $table = 'FooUser';
protected $primaryKey = 'foo_id_user'; // Place here the name of the id of your table
protected $fillable = [ // Insert here all the fillable fields of your users table
'field1',
'field2',
...
];
}
Note that the model now extends from Authenticatable instead of model, that is an alias for the class Illuminate\Foundation\Auth\User,so just use an alias for the User class: use Illuminate\Foundation\Auth\User as Authenticatable.
This is important!:
In order to use another field name for the password, add the function getAuthPassword into your newly created model, like bellow:
public function getAuthPassword(){
return $this->myPasswordField; // myPasswordField is the field on your users table for password
}
Now you have all of your model ready for the next step.
Second: On the file config > auth.php you have to find the providers array, and change the model property value to your model class name, for example:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\FooUser::class, // Place here the name of your model
],
],
All ready for the next step.
Third: Now you have to find the controller for the default Laravel Auth, located into app > http > controllers > auth > LoginController.php, and into the class, you only have to add a function called username that returns the username to be used for the auth action, like bellow:
class LoginController extends Controller{
..... // don't change other functions
public function username(){ // add the function username
return 'YourUsernameField';
}
}
Blade login template note: In your login blade template, you should change the username input name to the new one, and the password field name must be still password, thats because laravel use it for validation, but internally it's going to use your custom password field name.
And that's all. I hope this helps you.
Note: Never edit the files into the vendor folder!
For more information, read the documentation on the section Manually Authenticating Users
All of this was tested on Laravel 5.4 and 5.7 versions.
users.USERtousers.usercolumn in your migration script.users. See this post for more details.users.USERcolumn instead of checking theusers.usertable. I don't want to change my table schema.