DepartmentResource.php 808 B

12345678910111213141516171819202122232425262728293031
  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. 'manager_id' => $this->manager_id,
  20. 'children' => $this->when($this->children->isNotEmpty(),function (){
  21. return $this->children->map(function ($child){
  22. return new DepartmentResource($child);
  23. })->all();
  24. }),
  25. 'created_at' => (string)$this->created_at,
  26. ];
  27. }
  28. }