Home  >  Article  >  PHP Framework  >  How to change user authentication fields in Laravel

How to change user authentication fields in Laravel

PHPz
PHPzOriginal
2023-04-03 19:41:18675browse

Laravel is one of the most popular PHP frameworks today. Due to its excellent features, it is widely used in the development of web applications. Among them, user authentication is one of the most commonly used features in Laravel applications. Laravel provides a very complete user authentication system that can easily implement login, registration and other functions. But sometimes, we need to change the default authentication field. This article will introduce how to change the user authentication field in Laravel.

Laravel’s default user authentication field

The default user table in Laravel is users, which contains the following fields:

  • id : User ID
  • name: Username
  • email: Email
  • password : Password
  • remember_token: Remember password token

Among them, email and password are Laravel default User authentication fields to use. During the login and registration process, Laravel uses these two fields for authentication.

Change the default user authentication field

Sometimes, we need to change the default user authentication field to other fields, such as using the username field as the authentication field. In Laravel, we need to make changes to the following places:

Change the database migration file

First, we need to change the database migration file of the users table generated by Laravel , change the email field to the username field, such as the following migration file:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('username')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Change model file

Next, we need to change the Laravel generation The User model file, change the email field to the username field:

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    ...
    
    /**
     * Get the email address used for authentication.
     *
     * @return string
     */
    public function getEmailForAuthenticatioo()
    {
        return $this->username;
    }
    
    ...
}

Change Login Controller

Finally, We need to change the login controller to use the username field for authentication. In Laravel, the login controller defaults to app/Http/Controllers/Auth/LoginController.php. We need to add the following code to the file:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    ...
    
    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'username';
    }
    
    ...
}

At this point, we have completed all operations of using the username field as an authentication field. When a user logs in or registers, Laravel will use the username field for authentication.

Summary

The user authentication fields used by Laravel by default are email and password, however, sometimes we need to change the default authentication fields to other fields. This article explains how to change the user authentication field to the username field. By making changes to the database migration file, model file, and controller, we successfully implemented the username field as an authentication field. This action not only helps us meet special business needs, but also improves the user experience using the application.

The above is the detailed content of How to change user authentication fields in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn