RelatedParagraphResource.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\BookTitle;
  4. use App\Models\PaliText;
  5. use App\Models\Tag;
  6. use App\Models\TagMap;
  7. use Illuminate\Contracts\Support\Arrayable;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Resources\Json\JsonResource;
  10. class RelatedParagraphResource 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. $bookTitle = BookTitle::where('sn', $this['book_id'])->first();
  21. $data = [
  22. 'book' => $this['book'],
  23. 'para' => $this['para'],
  24. 'book_title_pali' => $bookTitle->title,
  25. 'cs6_para' => $this['cs6_para'],
  26. ];
  27. $paliTextUuid = PaliText::where('book', $bookTitle->book)->where('paragraph', $bookTitle->paragraph)->value('uid');
  28. $tagIds = TagMap::where('anchor_id', $paliTextUuid)->select('tag_id')->get();
  29. $data['tags'] = Tag::whereIn('id', $tagIds)->select('id', 'name', 'color')->get();
  30. $data['path'] = json_decode(PaliText::where('book', $this['book'])
  31. ->where('paragraph', $this['para'][0])
  32. ->value('path'));
  33. return $data;
  34. }
  35. }