RelationResource.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\UserApi;
  5. use App\Http\Api\ChannelApi;
  6. use App\Models\DhammaTerm;
  7. class RelationResource extends JsonResource
  8. {
  9. /**
  10. * Transform the resource into an array.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  14. */
  15. public function toArray($request)
  16. {
  17. $data = [
  18. "id"=>$this->id,
  19. "name"=> $this->name,
  20. "case"=> $this->case,
  21. "from"=> json_decode($this->from),
  22. "to"=> json_decode($this->to),
  23. "category"=> $this->category,
  24. "editor"=> UserApi::getByUuid($this->editor_id),
  25. "created_at"=> $this->created_at,
  26. "updated_at"=> $this->updated_at,
  27. ];
  28. $lang = $request->get('ui-lang');
  29. //TODO 默认英文
  30. $uiLang = strtolower($request->get('ui-lang','zh-Hans')) ;
  31. $term_channel = ChannelApi::getSysChannel("_System_Grammar_Term_{$uiLang}_");
  32. if($term_channel){
  33. $data['category_channel'] = $term_channel;
  34. if(!empty($this->category)){
  35. $term = DhammaTerm::where("word",$this->category)
  36. ->where('channal',$term_channel)
  37. ->first();
  38. if($term){
  39. $data['category_term']['channelId'] = $term_channel;
  40. $data['category_term']['word'] = $term->word;
  41. $data['category_term']['id'] = $term->guid;
  42. $data['category_term']['meaning'] = $term->meaning;
  43. }
  44. }
  45. }
  46. return $data;
  47. }
  48. }