SearchBookResource.php 1.7 KB

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