DiscussionResource.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. use App\Models\Sentence;
  7. use Illuminate\Support\Str;
  8. use App\Http\Api\MdRender;
  9. class DiscussionResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @param \Illuminate\Http\Request $request
  15. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  16. */
  17. public function toArray($request)
  18. {
  19. $data = [
  20. "id"=>$this->id,
  21. "title"=> $this->title,
  22. "content"=> $this->content,
  23. "content_type"=> $this->content_type,
  24. "parent"=> $this->parent,
  25. "status"=> $this->status,
  26. "editor"=> UserApi::getByUuid($this->editor_uid),
  27. "res_id"=>$this->res_id,
  28. "res_type"=> $this->res_type,
  29. "tpl_id"=> $this->tpl_id,
  30. 'children_count' => Discussion::where('parent',$this->id)->count(),
  31. "created_at"=> $this->created_at,
  32. "updated_at"=> $this->updated_at,
  33. ];
  34. $channels = array();
  35. switch ($this->res_type) {
  36. case 'sentence':
  37. $channelId = Sentence::where('uid',$this->res_id)->value('channel_uid');
  38. if(Str::isUuid($channelId)){
  39. $channels[] = $channelId;
  40. }
  41. break;
  42. default:
  43. # code...
  44. break;
  45. }
  46. if(count($channels)>0){
  47. $data["html"] = MdRender::render($this->content,$channels,null,'read');
  48. $data["summary"] = MdRender::render($this->content,$channels,null,'read','translation','markdown','text');
  49. }
  50. return $data;
  51. }
  52. }