1
votes

I am learning Laravel and LightHouse and I am trying to create a mutation that takes a DateTime and puts it into the database. Every time I do the mutation through the DateTime that input does not show up and is just labeled as null firing an error in the databases because the table does not allow the DateTime to be null. All of my other values work perfectly fine so I am very confused. The error GraphQL Playground throws is SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \"date_of_birth\" violates not-null constraint\nDETAIL: Failing row contains (17, Blaze, [email protected], null, ABadPassword, null, 2020-08-26 19:54:38, 2020-08-26 19:54:38, null). (SQL: insert into \"users\" (\"username\", \"password\", \"email\", \"updated_at\", \"created_at\") values (Blaze, ABadPassword, [email protected], 2020-08-26 19:54:38, 2020-08-26 19:54:38) returning \"id\")"

This Mutation that was attempted in GraphQL Playground:

mutation {
 createUser(
  username: "Blaze"
  email: "[email protected]"
  password: "ABadPassword"
  date_of_birth: "1998-01-16 00:00:00"
){
  id
 }
}

Here Is my schema:

"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")

"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")

type Mutation {
createUser(
    username: String @rules(apply: ["required", "unique:users,username"]),
    password: String @rules(apply: ["required"]),
    email: String @rules(apply: ["required", "unique:users,email"]),
    date_of_birth: DateTime @rules(apply: ["required"])
): User! @create
}

type User {
  id: ID!
  username: String!
  email: String!
  email_verified_at: DateTime
  password: String!
  created_at: DateTime!
  updated_at: DateTime!
  date_of_birth: DateTime!
}

Here is my User from the User.php file in the app:

<?php

namespace App;

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

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'username', 'email', 'password',
    'date_of_birth' => 'datetime'
];

/**
 * 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',
];
}

And here is my table in the database which was created by a migration:

DatabaseName=# SELECT * FROM users;
id | username | email | email_verified_at | password | remember_token | created_at | updated_at| 
date_of_birth  

Hopefully, this gives enough context for the bug, and thank you for your help in advance.

1
is the field date_of_birth nullable in your database ? - N69S
no date_of_birth is not nullable in the database that's why it throws the error. The issue is that I provide the date_of_birth in the mutation and laravel receives it because it's required by the rules but it sends a null value instead of inputted date to the database. This is the issue I am trying solve and its werid because all my other inputs work fine. - LucyEly

1 Answers

1
votes

I think your issue is in how you declared the $fillable of the model. You add an entry index 'date_of_birth' and value 'datetime' wich will make laravel think that the fillable value is datetime

change it to this

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'username', 'email', 'password', 'date_of_birth',
];