TermResource.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\ChannelApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\UserApi;
  7. use App\Http\Api\MdRender;
  8. class TermResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  15. */
  16. public function toArray($request)
  17. {
  18. $data = [
  19. "id"=>$this->id,
  20. "guid"=>$this->guid,
  21. "word"=> $this->word,
  22. "word_en"=> $this->word_en,
  23. "meaning"=> $this->meaning,
  24. "other_meaning"=> $this->other_meaning,
  25. "tag"=> $this->tag,
  26. "note"=> $this->note,
  27. "language"=> $this->language,
  28. "channal"=> $this->channal,
  29. "studio" => StudioApi::getById($this->owner),
  30. "editor"=> UserApi::getById($this->editor_id),
  31. "created_at"=> $this->created_at,
  32. "updated_at"=> $this->updated_at,
  33. ];
  34. if(!empty($this->channal)){
  35. $channelId = $this->channal;
  36. $data["channel"] = ChannelApi::getById($this->channal);
  37. }else{
  38. $channelId = ChannelApi::getSysChannel('_community_translation_'.$this->language.'_');
  39. if(empty($channelId)){
  40. $channelId = ChannelApi::getSysChannel('_community_translation_zh-hans_');
  41. }
  42. }
  43. if(!empty($this->note) && !empty($channelId)){
  44. $data["html"] = MdRender::render($this->note,[$channelId],null,'read');
  45. }
  46. return $data;
  47. }
  48. }