TocResource.php 2.0 KB

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