DiscussionResource.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\UserApi;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. use App\Models\Discussion;
  6. class DiscussionResource extends JsonResource
  7. {
  8. /**
  9. * Transform the resource into an array.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  13. */
  14. public function toArray($request)
  15. {
  16. $data = [
  17. "id"=>$this->id,
  18. "title"=> $this->title,
  19. "content"=> $this->content,
  20. "content_type"=> $this->content_type,
  21. "parent"=> $this->parent,
  22. "status"=> $this->status,
  23. "editor"=> UserApi::getByUuid($this->editor_uid),
  24. "res_id"=>$this->res_id,
  25. "res_type"=> $this->res_type,
  26. "created_at"=> $this->created_at,
  27. "updated_at"=> $this->updated_at,
  28. ];
  29. if(empty($this->parent)){
  30. $data['children_count'] = Discussion::where('parent',$this->id)->count();
  31. }
  32. return $data;
  33. }
  34. }