TocResource.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if (strpos($request->input('channels'), ',') === FALSE) {
  31. $channels = explode('_', $request->input('channels'));
  32. } else {
  33. $channels = explode(',', $request->input('channels'));
  34. }
  35. $title = ProgressChapter::where('book', $this->book)
  36. ->where('para', $this->paragraph)
  37. ->where('channel_id', $channels[0])
  38. ->whereNotNull('title')
  39. ->value('title');
  40. if (!empty($title)) {
  41. $data['title'] = $title;
  42. }
  43. //查询完成度
  44. foreach ($channels as $key => $channel) {
  45. $progress = ProgressChapter::where('book', $this->book)
  46. ->where('para', $this->paragraph)
  47. ->where('channel_id', $channel)
  48. ->value('progress');
  49. if ($progress) {
  50. $data['progress'][] = $progress;
  51. } else {
  52. $data['progress'][] = 0;
  53. }
  54. }
  55. }
  56. return $data;
  57. }
  58. }