Skip to content
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

[5.5] Show null if a relationship is loaded but has value of null #20969

Merged
merged 1 commit into from
Sep 4, 2017
Merged

[5.5] Show null if a relationship is loaded but has value of null #20969

merged 1 commit into from
Sep 4, 2017

Conversation

themsaid
Copy link
Member

@themsaid themsaid commented Sep 4, 2017

Let's say a Post belongs to an optional Author, so sometimes the author_id of a post is null.

Having the following in a resource:

"author" => AuthorResource::make($this->whenLoaded('author'))

In case author relationship is not loaded the author key won't appear in the response, in case the author is loaded and has value the AuthorResource will render the array format of the author.

However, in case the relationship is loaded but author_id = null, AuthorResource::make(null) will throw an exception.

This PR checks if any of the attributes holds a resource whose $resource attribute = null and just renders null to indicate that this post has no author.

"author" => null

@taylorotwell taylorotwell merged commit ffc31d0 into laravel:5.5 Sep 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants