SearchResource.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. "content"=> $this->content,
  19. "rank"=> $this->rank,
  20. "highlight"=> $this->highlight,
  21. ];
  22. $paliText = PaliText::where('book',$this->book)
  23. ->where('paragraph',$this->paragraph)
  24. ->first();
  25. if($paliText){
  26. $data['path'] = json_decode($paliText->path);
  27. if($paliText->level<100){
  28. $data["paliTitle"] = $paliText->toc;
  29. }else{
  30. $data["paliTitle"] = PaliText::where('book',$this->book)
  31. ->where('paragraph',$paliText->parent)
  32. ->value('toc');
  33. }
  34. }
  35. return $data;
  36. }
  37. }