NissayaEndingResource.php 1.4 KB

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