[英]Laravel / League Fractal returns an empty array instead of empty object
I'm having a problem with Fractal library.我在使用 Fractal 库时遇到了问题。 I'm using Spatie\\laravel-fractal library which is just wrapper for League\\fractal.
我正在使用 Spatie\\laravel-fractal 库,它只是 League\\fractal 的包装器。 So...
所以...
I want to return an empty object instead of an empty array.我想返回一个空对象而不是一个空数组。
public function includeDocumentType(Question $question)
{
return $question->documentType
? $this->item($question->documentType, new DocumentTypeTransformer)
: $this->null();
}
$this->null()
always returns []
after generation and I want to return {}
. $this->null()
总是在生成后返回[]
,我想返回{}
。 Tbh I want to have 2 functions like $this->nullItem()
and $this->nullCollection()
. Tbh 我想要两个函数,比如
$this->nullItem()
和$this->nullCollection()
。
Does anyone know what's the solution to this problem?有谁知道这个问题的解决方案是什么?
I just encountered the same problem.我刚刚遇到了同样的问题。 I wanted to return empty array instead of
NullResource
.我想返回空数组而不是
NullResource
。 Here is what I was able to find from the source code.这是我从源代码中找到的内容。
Instead of $this->null()
, $this->primitive(null)
did the trick.而不是
$this->null()
, $this->primitive(null)
做到了这一点。 You can enter whatever you want to be returned.您可以输入任何您想要返回的内容。
In your case $this->primitive(new \\stdClass())
should be the way to go.在你的情况下
$this->primitive(new \\stdClass())
应该是要走的路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.