首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >突变中的Laravel LightHouse GraphQL DateTime输入始终为空

突变中的Laravel LightHouse GraphQL DateTime输入始终为空
EN

Stack Overflow用户
提问于 2020-08-27 04:07:30
回答 1查看 563关注 0票数 1

我正在学习Laravel和LightHouse,我正在尝试创造一种突变,将DateTime放入数据库中。每次我通过DateTime进行突变时,输入没有显示,只是被标记为null,这会在数据库中触发一个错误,因为表不允许DateTime为null。我所有的其他值都工作得很好,所以我很困惑。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平台抛出的错误

在GraphQL游乐场中尝试的这种突变:

代码语言:javascript
运行
复制
mutation {
 createUser(
  username: "Blaze"
  email: "blaze@test.com"
  password: "ABadPassword"
  date_of_birth: "1998-01-16 00:00:00"
){
  id
 }
}

以下是我的方案:

代码语言:javascript
运行
复制
"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文件:

代码语言:javascript
运行
复制
<?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',
];
}

下面是我在数据库中的表,它是通过迁移创建的:

代码语言:javascript
运行
复制
DatabaseName=# SELECT * FROM users;
id | username | email | email_verified_at | password | remember_token | created_at | updated_at| 
date_of_birth  

希望这能为bug提供足够的背景信息,并提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2020-08-27 05:06:33

我认为您的问题在于如何声明模型的$fillable。您添加了一个条目索引'date_of_birth'和值'datetime',这将使laravel认为可填充的值为datetime

把它改成这样

代码语言:javascript
运行
复制
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'username', 'email', 'password', 'date_of_birth',
];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63605047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档