SearchBookResource.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\BookTitle;
  4. use App\Models\PaliText;
  5. use App\Models\TagMap;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Resources\Json\JsonResource;
  9. use Illuminate\Support\Facades\Log;
  10. class SearchBookResource extends JsonResource
  11. {
  12. /**
  13. * Transform the resource into an array.
  14. *
  15. * @param Request $request
  16. * @return array|Arrayable|\JsonSerializable
  17. */
  18. public function toArray($request)
  19. {
  20. $book = BookTitle::where('sn', $this->pcd_book_id)->first();
  21. $data = [
  22. 'pcdBookId' => $this->pcd_book_id,
  23. 'count' => $this->co,
  24. ];
  25. if ($book) {
  26. $toc = PaliText::where('book', $book->book)
  27. ->where('paragraph', $book->paragraph)
  28. ->value('toc');
  29. $data['book'] = $book->book;
  30. $data['paragraph'] = $book->paragraph;
  31. $data['paliTitle'] = $toc;
  32. // tags
  33. $data['tags'] = $this->getTags($book->book, $book->paragraph);
  34. } else {
  35. Log::error('book title is null pcd_book_id='.$this->pcd_book_id);
  36. $data['book'] = 0;
  37. $data['paragraph'] = 0;
  38. $data['paliTitle'] = '';
  39. }
  40. return $data;
  41. }
  42. private function getTags(string $book, string $para)
  43. {
  44. $uid = PaliText::where('book', $book)->where('paragraph', $para)->first()->uid;
  45. $tagsName = TagMap::where('table_name', 'pali_texts')
  46. ->where('anchor_id', $uid)
  47. ->join('tags', 'tag_maps.tag_id', '=', 'tags.id')
  48. ->select('tags.name')
  49. ->get();
  50. return $tagsName;
  51. }
  52. }