grunburg
1 month ago
238
2
Servers

SQLSTATE: Not null violation: 7

Posted 1 month ago by grunburg

I'm making an API authentication system. Currently testing register action, I ran into this problem, that I can't solve:

SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"handle\" violates not-null constraint\nDETAIL:  Failing row contains (12, Test, null, [email protected], null, password, null, 2020-09-02 10:59:53, 2020-09-02 10:59:53). (SQL: insert into \"users\" (\"email\", \"name\", \"password\", \"updated_at\", \"created_at\") values ([email protected], Test, password, 2020-09-02 10:59:53, 2020-09-02 10:59:53) returning \"id\")

Im posting data from Postman with all the keys and values, including handle (@username).

Authcontroller.php

class AuthController extends Controller
{
  public function register(Request $request)
  {
    $data = $request->validate([
      'email' => ['email', 'unique:users', 'required'],
      'name' => ['string', 'max:225', 'required'],
      'handle' => ['string', 'alpha_num', 'unique:users', 'max:16', 'required'],
      'password' => ['confirmed', 'required']
    ]);

    $user = User::create($data);

    $token = $user->createToken('Auth')->accessToken;

    return response(['user' => $user, 'token' => $token], 201);
  }
}

migration file:

public function up()
{
  Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('handle')->unique();
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
  });
}

Is there some kind of typo I don't see or just something wrong with my code?

Please sign in or create an account to participate in this conversation.