SearchResource.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Models\PaliText;
  5. class SearchResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  12. */
  13. public function toArray($request)
  14. {
  15. $data = [
  16. "book"=>$this->book,
  17. "paragraph"=> $this->paragraph,
  18. ];
  19. if(isset($this->rank)){
  20. $data["rank"] = $this->rank;
  21. }
  22. $paliText = PaliText::where('book',$this->book)
  23. ->where('paragraph',$this->paragraph)
  24. ->first();
  25. if(isset($this->highlight)){
  26. $data["highlight"] = $this->highlight;
  27. }else if(isset($this->content)){
  28. $data["content"] = $this->content;
  29. }else{
  30. $data["content"] = $paliText->html;
  31. }
  32. if($paliText){
  33. $data['path'] = json_decode($paliText->path);
  34. if($paliText->level<100){
  35. $data["paliTitle"] = $paliText->toc;
  36. }else{
  37. $data["paliTitle"] = PaliText::where('book',$this->book)
  38. ->where('paragraph',$paliText->parent)
  39. ->value('toc');
  40. }
  41. }
  42. return $data;
  43. }
  44. }