RelatedParagraphResource.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 App\Models\Tag;
  8. use Illuminate\Support\Facades\Log;
  9. class RelatedParagraphResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @param \Illuminate\Http\Request $request
  15. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  16. */
  17. public function toArray($request)
  18. {
  19. $bookTitle = BookTitle::where('sn',$this["book_id"])->first();
  20. $data = [
  21. "book"=>$this['book'],
  22. "para"=> $this['para'],
  23. "book_title_pali"=> $bookTitle->title,
  24. 'cs6_para'=> $this['cs6_para'],
  25. ];
  26. $paliTextUuid = PaliText::where('book',$bookTitle->book)->where('paragraph',$bookTitle->paragraph)->value('uid');
  27. $tagIds = TagMap::where('anchor_id',$paliTextUuid)->select('tag_id')->get();
  28. $data['tags'] = Tag::whereIn('id',$tagIds)->select('id','name','color')->get();
  29. $data['path'] = json_decode(PaliText::where('book',$this['book'])
  30. ->where('paragraph',$this['para'][0])
  31. ->value('path'));
  32. return $data;
  33. }
  34. }