SearchBookResource.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 Illuminate\Support\Facades\Log;
  7. class SearchBookResource 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. $book = BookTitle::where('sn', $this->pcd_book_id)->first();
  18. $data = [
  19. 'pcdBookId' => $this->pcd_book_id,
  20. "count" => $this->co,
  21. ];
  22. if ($book) {
  23. $toc = PaliText::where('book', $book->book)
  24. ->where("paragraph", $book->paragraph)
  25. ->value('toc');
  26. $data["book"] = $book->book;
  27. $data["paragraph"] = $book->paragraph;
  28. $data["paliTitle"] = $toc;
  29. } else {
  30. Log::error('book title is null pcd_book_id=' . $this->pcd_book_id);
  31. $data["book"] = 0;
  32. $data["paragraph"] = 0;
  33. $data["paliTitle"] = '';
  34. }
  35. return $data;
  36. }
  37. }