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] Add ability to pass callback to whenLoaded Resource method #21490

Merged
merged 2 commits into from Oct 2, 2017

Conversation

Projects
None yet
3 participants
@aaronhuisinga
Contributor

aaronhuisinga commented Oct 1, 2017

Currently, the whenLoaded method for API Resources is only able to return the relationship object.
A similar method, whenPivotLoaded, allows a callback to be passed, which will be added to the returned array.
This PR adds this similar functionality to the whenLoaded method. If a callback is passed, the return value of that callback will be added to the array to be returned. If no callback is passed, the relationship object is returned, as is the current behavior. This should be a fully backward compatible addition.

Behavior when no callback is specified:

/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request
 * @return array
 */
public function toArray($request)
{
    return [
        'id' => $this->id,
        'title' => $this->title,
        'author' => new AuthorResource($this->whenLoaded('author')),
    ];
}

// Would return
[
    'id' => 1,
    'title' => 'Post Title',
    'author' => ['id' => 1, 'name' => 'Taylor Otwell'],
]

Behavior when a callback is specified:

/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request
 * @return array
 */
public function toArray($request)
{
    return [
        'id' => $this->id,
        'title' => $this->title,
        'author_name' => $this->whenLoaded('author'), function() {
            return $this->author->name;
        }),
    ];
}

// Would return
[
    'id' => 1,
    'title' => 'Post Title',
    'author_name' => 'Taylor Otwell',
]

This isn't the most practical example ever, but being able to pass a callback is very handy for things such as counts or other adjustments to the related object that don't necessarily need to be part of a dedicated Resource object for a model.

@deleugpn

This comment has been minimized.

Show comment
Hide comment
@deleugpn

deleugpn Oct 1, 2017

Contributor

Can the same result be achieved with with attributes?

Contributor

deleugpn commented Oct 1, 2017

Can the same result be achieved with with attributes?

@aaronhuisinga

This comment has been minimized.

Show comment
Hide comment
@aaronhuisinga

aaronhuisinga Oct 1, 2017

Contributor

@deleugpn I do not believe so. I believe the only method that checks whether or not a relationship has been loaded is the isLoaded method. Everything else I've been able to find either causes an Exception if the relationship is not loaded, or else causes an n+1 problem.

Contributor

aaronhuisinga commented Oct 1, 2017

@deleugpn I do not believe so. I believe the only method that checks whether or not a relationship has been loaded is the isLoaded method. Everything else I've been able to find either causes an Exception if the relationship is not loaded, or else causes an n+1 problem.

@taylorotwell taylorotwell merged commit 32efb56 into laravel:5.5 Oct 2, 2017

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment