RelationResource.php 2.5 KB

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