TocResource.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Models\ProgressChapter;
  5. class TocResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  12. */
  13. public function toArray($request)
  14. {
  15. $data = [
  16. "book"=>$this->book,
  17. "paragraph"=> $this->paragraph,
  18. "pali_title"=> $this->toc,
  19. "level"=>$this->level
  20. ];
  21. $title= ProgressChapter::where('book',$this->book)
  22. ->where('para',$this->paragraph)
  23. ->where('lang','zh')
  24. ->whereNotNull('title')
  25. ->value('title');
  26. if(!empty($title)){
  27. $data['title'] = $title;
  28. }
  29. if($request->has('channels')){
  30. $channels = explode(',',$request->get('channels'));
  31. $title= ProgressChapter::where('book',$this->book)
  32. ->where('para',$this->paragraph)
  33. ->where('channel_id',$channels[0])
  34. ->whereNotNull('title')
  35. ->value('title');
  36. if(!empty($title)){
  37. $data['title'] = $title;
  38. }
  39. //查询完成度
  40. foreach ($channels as $key => $channel) {
  41. $progress= ProgressChapter::where('book',$this->book)
  42. ->where('para',$this->paragraph)
  43. ->where('channel_id',$channel)
  44. ->value('progress');
  45. if($progress){
  46. $data['progress'][] = $progress;
  47. }else{
  48. $data['progress'][] = 0;
  49. }
  50. }
  51. }
  52. return $data;
  53. }
  54. }