Skip to content

After #51342 whenLoaded doesn't return null when needed #55374

Open
@jeanroloff

Description

@jeanroloff

Laravel Version

11.20.0

PHP Version

8.3.16

Database Driver & Version

No response

Description

Before version 11.20.0 the following code would return null

class CommentResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'value' => $this->whenLoaded('entity', $this->resource->entity->comment),
        ];
    }
}

If $this->resource->entity->comment is actually null on the database, the code correctly returns the following output in the api.

{
"value": null
}

But after the related issue got merged #51342, the code doenst return null anymore; it returns the entity that is being loaded, which caused unexpected results in our source code today.

{
"value": {
// entity object
}
}

I really think that we should be able to return null if the value is actually null when using the whenLoaded function

Steps To Reproduce

Check description.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions