NissayaEndingResource.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 NissayaEndingResource 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. "ending"=> $this->ending,
  20. "lang"=> $this->lang,
  21. "relation"=> $this->relation,
  22. "case"=> $this->case,
  23. "from"=> json_decode($this->from),
  24. "count"=> $this->count,
  25. "editor"=> UserApi::getByUuid($this->editor_id),
  26. "created_at"=> $this->created_at,
  27. "updated_at"=> $this->updated_at,
  28. ];
  29. if($this->lang === 'my'){
  30. $uiLang = strtolower($request->get('ui-lang','en')) ;
  31. $term_channel = ChannelApi::getSysChannel("_System_Grammar_Term_{$uiLang}_");
  32. if($term_channel){
  33. $term = DhammaTerm::where("word",$this->ending)
  34. ->where('channal',$term_channel)
  35. ->first();
  36. $data['term_channel'] = $term_channel;
  37. if($term){
  38. $data['term_id'] = $term->guid;
  39. }
  40. }
  41. }
  42. return $data;
  43. }
  44. }