NissayaEndingResource.php 1.5 KB

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