我有一个关于Fractal库的问题。我使用的是Spatie\laravel fractal库,它只是\League的包装器。所以..。
我想返回一个空对象,而不是一个空数组。
public function includeDocumentType(Question $question)
{
return $question->documentType
? $this->item($question->documentType, new DocumentTypeTransformer)
: $this->null();
}$this->null()在生成后总是返回[],而我想返回{}。我想有像$this->nullItem()和$this->nullCollection()这样的2个函数。
有人知道这个问题的解决方案是什么吗?
发布于 2020-11-20 02:00:18
我刚刚遇到了同样的问题。我想返回空数组而不是NullResource。这是我能从源代码中找到的东西。
$this->primitive(null)做到了这一点,而非$this->null()。您可以输入希望返回的任何内容。
在您的情况下,$this->primitive(new \stdClass())应该是最佳选择。

https://stackoverflow.com/questions/54138227
复制相似问题