簡體   English   中英

Mutation 中的 Laravel LightHouse GraphQL DateTime 輸入始終為空

[英]Laravel LightHouse GraphQL DateTime Input in Mutation Is Always Null

我正在學習 Laravel 和 LightHouse,我正在嘗試創建一個使用 DateTime 並將其放入數據庫的突變。 每次我通過 DateTime 進行突變時,輸入都不會顯示,並且只是標記為空,在數據庫中觸發錯誤,因為該表不允許 DateTime 為空。 我所有的其他值都工作得很好,所以我很困惑。 GraphQL Playground 拋出的錯誤是SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \\"date_of_birth\\" violates not-null constraint\\nDETAIL: Failing row contains (17, Blaze, blaze@test.com, 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, blaze@test.com, 2020-08-26 19:54:38, 2020-08-26 19:54:38) returning \\"id\\")" SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \\"date_of_birth\\" violates not-null constraint\\nDETAIL: Failing row contains (17, Blaze, blaze@test.com, 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, blaze@test.com, 2020-08-26 19:54:38, 2020-08-26 19:54:38) returning \\"id\\")"

在 GraphQL Playground 中嘗試的這個 Mutation:

mutation {
 createUser(
  username: "Blaze"
  email: "blaze@test.com"
  password: "ABadPassword"
  date_of_birth: "1998-01-16 00:00:00"
){
  id
 }
}

這是我的架構:

"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!
}

這是應用程序中 User.php 文件中的我的用戶:

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

這是我在數據庫中由遷移創建的表:

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

希望這為錯誤提供了足夠的上下文,並提前感謝您的幫助。

我認為您的問題在於您如何聲明模型的$fillable 您添加一個條目索引'date_of_birth'和值'datetime'這將使 Laravel 認為可填充值是datetime

改成這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM