RelationResource.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. $data["editor"] = UserApi::getByUuid($this->editor_id);
  30. $uiLang = strtolower($request->input('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. $data['name_channel'] = $term_channel;
  46. $term_name = DhammaTerm::where("word", $this->name)
  47. ->where('channal', $term_channel)
  48. ->first();
  49. if ($term_name) {
  50. $data['name_term']['channelId'] = $term_channel;
  51. $data['name_term']['word'] = $term_name->word;
  52. $data['name_term']['id'] = $term_name->guid;
  53. $data['name_term']['meaning'] = $term_name->meaning;
  54. }
  55. }
  56. }
  57. /*
  58. */
  59. return $data;
  60. }
  61. }