RelationResource.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "match"=> json_decode($this->match),
  24. "category"=> $this->category,
  25. "editor"=> UserApi::getByUuid($this->editor_id),
  26. "created_at"=> $this->created_at,
  27. "updated_at"=> $this->updated_at,
  28. ];
  29. $lang = $request->get('ui-lang');
  30. //TODO 默认英文
  31. $uiLang = strtolower($request->get('ui-lang','zh-Hans')) ;
  32. $term_channel = ChannelApi::getSysChannel("_System_Grammar_Term_{$uiLang}_");
  33. if($term_channel){
  34. $data['category_channel'] = $term_channel;
  35. if(!empty($this->category)){
  36. $term = DhammaTerm::where("word",$this->category)
  37. ->where('channal',$term_channel)
  38. ->first();
  39. if($term){
  40. $data['category_term']['channelId'] = $term_channel;
  41. $data['category_term']['word'] = $term->word;
  42. $data['category_term']['id'] = $term->guid;
  43. $data['category_term']['meaning'] = $term->meaning;
  44. }
  45. }
  46. $data['name_channel'] = $term_channel;
  47. $term_name = DhammaTerm::where("word",$this->name)
  48. ->where('channal',$term_channel)
  49. ->first();
  50. if($term_name){
  51. $data['name_term']['channelId'] = $term_channel;
  52. $data['name_term']['word'] = $term_name->word;
  53. $data['name_term']['id'] = $term_name->guid;
  54. $data['name_term']['meaning'] = $term_name->meaning;
  55. }
  56. }
  57. return $data;
  58. }
  59. }