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

[7.x] Conditionally returning appended attributes in API resources #33422

Merged
merged 8 commits into from
Jul 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add whenAppended() method for resources
  • Loading branch information
jessarcher committed Jul 3, 2020
commit 901b8c390951d6f1e09bf56e604cee8719a8d98d
17 changes: 17 additions & 0 deletions src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ protected function whenPivotLoadedAs($accessor, $table, $value, $default = null)
);
}

/**
* Retrieve an accessor when it has been appended.
*
* @param string $attribute
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenAppended($attribute, $value = null, $default = null)
{
if ($this->resource->hasAppended($attribute)) {
return func_num_args() >= 2 ? value($value) : $this->resource->$attribute;
}

return func_num_args() === 3 ? value($default) : new MissingValue;
}

/**
* Transform the given value if it is present.
*
Expand Down