Home > Blockchain >  How can I change the filed email from Larval 8 auth user table to username?
How can I change the filed email from Larval 8 auth user table to username?

Time:03-14

  1. currently, I'm trying to change the field email in user table to username. But seems like it did not work at all. What I did just replace the email (inside of $fillable array) to username.

2.Here is the code for User.php file.

<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'username',
        'password',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    
    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier() {
        return $this->getKey();
    }
    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims() {
        return [];
    }

    public function username()
    {
        return 'username';
    }
}

Here is the schema for my user table enter image description here

CodePudding user response:

For this situtation you need to update your migration file for this, then you can use your username without e-mail.

CodePudding user response:

You also have to update your user table migration. If you don't need the email field, you can replace the line :

$table->string('email')->unique();

with :

$table->string('username');

Then, run :

php artisan migrate:refresh
  • Related