Php 变异中的Laravel LightHouse GraphQL DateTime输入始终为空
我正在学习拉威尔和灯塔,我正试图创造一种变异,它需要一个日期时间,并将其输入数据库。每次我在DateTime中执行变异时,输入都不会显示,只是被标记为null,并在数据库中触发一个错误,因为表不允许DateTime为null。我所有其他的价值观都很好,所以我很困惑。错误图形为Php 变异中的Laravel LightHouse GraphQL DateTime输入始终为空,php,laravel,postgresql,graphql,laravel-lighthouse,Php,Laravel,Postgresql,Graphql,Laravel Lighthouse,我正在学习拉威尔和灯塔,我正试图创造一种变异,它需要一个日期时间,并将其输入数据库。每次我在DateTime中执行变异时,输入都不会显示,只是被标记为null,并在数据库中触发一个错误,因为表不允许DateTime为null。我所有其他的价值观都很好,所以我很困惑。错误图形为SQLSTATE[23502]:非空冲突:7错误:列“出生日期”中的空值违反非空约束\n详细信息:失败的行包含(17,Blaze,blaze@test.com,null,ABadPassword,null,2020-08-2
SQLSTATE[23502]:非空冲突:7错误:列“出生日期”中的空值违反非空约束\n详细信息:失败的行包含(17,Blaze,blaze@test.com,null,ABadPassword,null,2020-08-26 19:54:382020-08-26 19:54:38,null)。(SQL:插入“用户”(“用户名”、“密码”、“电子邮件”、“更新地址”、“创建地址”)值(Blaze、ABadPassword、,blaze@test.com,2020-08-26 19:54:382020-08-26 19:54:38)返回“id”
在GraphQL中尝试的这种突变:
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',
];
}
希望这能为该bug提供足够的上下文,并提前感谢您的帮助。我认为您的问题在于如何声明模型的
$filleble
。您添加了一个条目索引'date\u of_birth'
和value'datetime'
,这会让laravel认为可填充的值是datetime
换成这个
/**
*质量可分配的属性。
*
*@var数组
*/
受保护的$fillable=[
“用户名”、“电子邮件”、“密码”、“出生日期”,
];
数据库中的出生日期
字段是否可以为空?否出生日期
在数据库中不可以为空,这就是它抛出错误的原因。问题是我在变异中提供了出生日期
,laravel收到它,因为规则要求它,但它发送了一个空值,而不是输入的date到数据库。这是我正在尝试解决的问题,它是werid,因为我的所有其他输入都可以正常工作。此外,我尝试将它移动到强制转换,因为正如您所说,它应该作为字符串进行强制转换和新迁移,但这给我留下了相同的错误,所以我现在将保持它的可填充状态。@LucyEly…我删除了我的疑问..自定义scalar负责转换,这是有意义的,因为大多数GraphQL自定义日期时间标量包都这样做
DatabaseName=# SELECT * FROM users;
id | username | email | email_verified_at | password | remember_token | created_at | updated_at|
date_of_birth