-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Add
whenHas
to JsonResource (#45376)
* Add whenHas * Add tests * enhancement `whenCounted` * Formatting * Fix * Replace isset function to `Arr::has()` * formatting * formatting * adjust wording * formatting Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
651b149
commit b83a1ce
Showing
3 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/Integration/Http/Fixtures/PostResourceWithOptionalHasAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Http\Fixtures; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class PostResourceWithOptionalHasAttributes extends JsonResource | ||
{ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'first' => $this->whenHas('is_published'), | ||
'second' => $this->whenHas('is_published', 'override value'), | ||
'third' => $this->whenHas('is_published', function () { | ||
return 'override value'; | ||
}), | ||
'fourth' => $this->whenHas('is_published', $this->is_published, 'default'), | ||
'fifth' => $this->whenHas('is_published', $this->is_published, function () { | ||
return 'default'; | ||
}), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters