2
0

DiscussionResource.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "type"=> $this->type,
  30. "tpl_id"=> $this->tpl_id,
  31. 'children_count' => Discussion::where('parent',$this->id)->count(),
  32. "created_at"=> $this->created_at,
  33. "updated_at"=> $this->updated_at,
  34. ];
  35. $channels = array();
  36. switch ($this->res_type) {
  37. case 'sentence':
  38. $channelId = Sentence::where('uid',$this->res_id)->value('channel_uid');
  39. if(Str::isUuid($channelId)){
  40. $channels[] = $channelId;
  41. }
  42. break;
  43. default:
  44. # code...
  45. break;
  46. }
  47. if(count($channels)>0){
  48. $data["html"] = MdRender::render($this->content,$channels,null,'read');
  49. $data["summary"] = MdRender::render($this->content,$channels,null,'read','translation','markdown','text');
  50. }
  51. return $data;
  52. }
  53. }