2
0

RelationResource.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * The payload must contain only scalars and arrays: it is cached by
  13. * RelationController, and Laravel 13 refuses to unserialize objects from
  14. * cache unless they are listed in cache.serializable_classes. Hence the
  15. * associative json_decode() and the ISO-8601 date strings.
  16. *
  17. * @param \Illuminate\Http\Request $request
  18. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  19. */
  20. public function toArray($request)
  21. {
  22. $data = [
  23. "id" => $this->id,
  24. "name" => $this->name,
  25. "case" => $this->case,
  26. "from" => json_decode((string) $this->from, true),
  27. "to" => json_decode((string) $this->to, true),
  28. "match" => json_decode((string) $this->match, true),
  29. "category" => $this->category,
  30. "created_at" => $this->created_at?->toISOString(),
  31. "updated_at" => $this->updated_at?->toISOString(),
  32. ];
  33. if (!$request->has('vocabulary')) {
  34. $data["editor"] = UserApi::getByUuid($this->editor_id);
  35. $uiLang = strtolower($request->input('ui-lang', 'zh-Hans'));
  36. $term_channel = ChannelApi::getSysChannel("_System_Grammar_Term_{$uiLang}_");
  37. if ($term_channel) {
  38. $data['category_channel'] = $term_channel;
  39. if (!empty($this->category)) {
  40. $term = DhammaTerm::where("word", $this->category)
  41. ->where('channal', $term_channel)
  42. ->first();
  43. if ($term) {
  44. $data['category_term']['channelId'] = $term_channel;
  45. $data['category_term']['word'] = $term->word;
  46. $data['category_term']['id'] = $term->guid;
  47. $data['category_term']['meaning'] = $term->meaning;
  48. }
  49. }
  50. $data['name_channel'] = $term_channel;
  51. $term_name = DhammaTerm::where("word", $this->name)
  52. ->where('channal', $term_channel)
  53. ->first();
  54. if ($term_name) {
  55. $data['name_term']['channelId'] = $term_channel;
  56. $data['name_term']['word'] = $term_name->word;
  57. $data['name_term']['id'] = $term_name->guid;
  58. $data['name_term']['meaning'] = $term_name->meaning;
  59. }
  60. }
  61. }
  62. /*
  63. */
  64. return $data;
  65. }
  66. }