-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Laravel - $this->null() returns an empty array instead of empty object #469
Comments
Withouth looking further into the issue, can you check if this is duplicate of #433 ? |
@lordrhodos I think it's a different issue, because, I can't override anything. The only object this function is waiting to be returned is |
How about returning an object |
The only object this function is waiting to be returned is |
$this->null()
returns an empty array instead of empty object
$this->null()
returns an empty array instead of empty object
have you tried |
@matthewtrask I have still tried and got this error: |
What serializer are you using? What specific output are you hoping for? |
I'm also using the default serializer. The most optinal, non-breaking and clean solution would be to allow the following two methods: |
Hello @buglinjo , @BRafols
And it works as expected for me. But if you try to change null to '{}' or 'new \stdClass()' it will throw an error @KorvinSzanto , and it may cause an issue.
Also to return [] for empty collection you can just use anything like this
and it will return |
So... Conclusion... We need this function to be fixed:
public function toJson($options = 0)
{
return json_encode($this->toArray(), $options);
} To public function toJson($options = 0)
{
return json_encode((object)$this->toArray(), $options);
} Idk if it will break anything though, but it fixes my problem if I do that. |
Is there any solution to this? I have an item include which obviously returns an object but when I have no data for this, I want an empty object, not an empty array. I have created my own ArraySerializer as I needed to anyway and I tried overriding the null function with
Update: I just decided to fork the package and add my own resource type |
We are working on a 1.0 for this package and looking to include something like this in the Scope class. |
Any updates on this? |
I too am looking for an update on this. I'd personally like a null item resource to look like so: {
"key": {
"data: null
}
} An empty array or object doesn't make any sense for a non-existent object. |
Any updates on this? Would be nice to have to prevent additional if checks... Ty |
Any solution? |
I have an almost working POC. Im working with the other maintainers to figure out how to release it. The plan is that the The issue though is that this is a breaking change, so I dont want to unilaterally release something like this. Thanks for your patience! |
I dont know if solve your problem. But you can try
And return is
this is dont {} but is better than [] |
yea weirdly enough, i expected now i make the code like this to make it work:
|
@lucasres found the solution to this issue. Thank you! Closing. |
I'm having a problem with Fractal library. I'm using
spatie\laravel-fractal
library which is just wrapper forthephpleague/fractal
. So...I want to return an empty object instead of an empty array.
$this->null()
always returns[]
after generation and I want to return{}
. Tbh I want to have 2 functions like$this->nullItem()
and$this->nullCollection()
.Does anyone know what's the solution to this problem?
The text was updated successfully, but these errors were encountered: