我有一个带日期字段的 Laravel 模型,archivedAt
。这已在模型的 $dates
数组中设置,如下所示:
联系人模型
class Contact extends Model
{
/**
* The name of the table in the database.
*
* @laravel-table-name
*
* @var string
*/
protected $table = 'Contacts';
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'createdAt',
'updatedAt',
'archivedAt'
];
}
当我尝试通过 GraphQL 向服务器发送 JSON 负载时,我收到以下响应:
{
"data": {
"editContact": null
},
"errors": [
{
"message": "Unexpected data found.\nTrailing data",
"locations": [
{
"line": 2,
"column": 3
}
]
}
]
}
当然,Laravel 的 GraphQL 库很糟糕,不包括完整的堆栈跟踪或任何附加信息,但这似乎是我设置 archivedAt
字段的问题。我是这样做的:
{
"operationName": null,
"variables": {
"contactEditForm": {
"id": "2",
"name": "Maybelle Collier",
"email": "cletus49@example.org",
"phone": "+1 (997) 381-8483",
"archivedAt": "2018-03-19T00:07:57.191Z",
"createdAt": "2018-03-18 23:57:30",
"updatedAt": "2018-03-18 23:57:30"
}
},
"query": "mutation ($contactEditForm: ContactEditForm!) {\n editContact(contactEditForm: $contactEditForm) {\n id\n name\n __typename\n }\n}\n"
}
如您所见,archivedAt
是一个 ISO8601 格式的字符串,它是在我的客户端上生成的:
(new Date())->toISOString();
然后应该将其保存在带有突变的 GraphQL 服务器上,如下所示:
public function resolve($root, $args, $context, ResolveInfo $info)
{
$contact = Contact::find($args['contactEditForm']['id']);
$contact->fill($args['contactEditForm']);
$contact->save();
return $contact;
}
为什么 Laravel 不喜欢这样?
最佳答案
我从来没有遇到过这个问题,但是根据 Laravel's Docs ,如果您在 $dates 属性中设置该字段,则可以正确更改:
When a column is considered a date, you may set its value to a UNIX timestamp, date string (Y-m-d), date-time string, and of course a DateTime / Carbon instance, and the date's value will automatically be correctly stored in your database:
因此,显然,增变器不允许使用 ISO 字符串日期,因此您要么将其设置为这些格式之一,defining an acessor ( like this ) 或将其从 $dates 中删除并自行处理
关于javascript - Laravel 在向服务器发送 JSON 格式的 ISO 格式日期时提示 'trailing data'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354265/