DepartmentResource.php 901 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: kelyliang
  5. * Date: 2024/2/29
  6. * Time: 上午 11:46
  7. */
  8. namespace App\Http\Resources\API;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Resources\Json\JsonResource;
  11. class DepartmentResource extends JsonResource
  12. {
  13. public function toArray(Request $request): array
  14. {
  15. return[
  16. 'id' => $this->id,
  17. 'name' => $this->name,
  18. 'parent_id' => $this->parent_id,
  19. 'count' => $this->users->count(),
  20. 'manager_id' => $this->manager_id ? new UserProfileResource($this->byManager) : 0,
  21. 'children' => $this->when($this->children->isNotEmpty(),function (){
  22. return $this->children->map(function ($child){
  23. return new DepartmentResource($child);
  24. })->all();
  25. }),
  26. 'created_at' => (string)$this->created_at,
  27. ];
  28. }
  29. }