Search code examples
sqllaraveleloquentlaravel-authentication

Laravel Auth register user failing


I'm using Laravel's Auth scaffolding. When trying to register (create) a new user I'm getting the following error (from the logs):

Integrity constraint violation: 19 NOT NULL constraint failed: users.password

but password is part of $hidden array.

Here's my model:

<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use Notifiable;

    const USER_FIRST_NAME_FIELD        = "first_name";
    const USER_LAST_NAME_FIELD         = "last_name";
    const USER_PREFERRED_NAME_FIELD    = "preferred_name";
    const USER_EMAIL_FIELD             = "email";
    const USER_EMAIL_VERIFIED_AT_FIELD = "email_verified_at";
    const USER_PASSWORD_FIELD          = "password";
    const USER_REMEMBER_TOKEN_FIELD    = "remember_token";
    const USER_RECEIVE_NEWSLETTER_FIELD= "receive_newsletter";
    const USER_ACTIVE_FIELD            = "active";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        self::USER_FIRST_NAME_FIELD, 
        self::USER_LAST_NAME_FIELD,
        self::USER_PREFERRED_NAME_FIELD,
        self::USER_EMAIL_FIELD,
        self::USER_RECEIVE_NEWSLETTER_FIELD,
        self::USER_ACTIVE_FIELD,
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        self::USER_PASSWORD_FIELD,
        self::USER_REMEMBER_TOKEN_FIELD
    ];
}

and here's the create method in RegisterController:

protected function create(array $data) : User
    {
        return User::create([
            'first_name'         => $data['first_name'],
            'last_name'          => $data['last_name'],
            'preferred_name'     => $data['preferred_name'],
            'email'              => $data['email'],
            'password'           => Hash::make($data['password']),
            'receive_newsletter' => !isset($data['receive_newsletter']) || $data['receive_newsletter'] != 'on' ? 0 : 1,
        ]);
    }

If I print out $data at the beginning of this method I can see that password is there.

Any suggestions?


Solution

  • its because You haven't added password in protected $fillable, Try updating fillable with password