Chapter.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Repositories\Models\Course;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Model;
  5. use Carbon\Carbon;
  6. use Jenssegers\Mongodb\Eloquent\SoftDeletes;
  7. use Prettus\Repository\Contracts\Transformable;
  8. use Prettus\Repository\Traits\TransformableTrait;
  9. /**
  10. * Class CourseChapter.
  11. *
  12. * @package namespace App\Repositories\Models;
  13. */
  14. class Chapter extends Model implements Transformable
  15. {
  16. use TransformableTrait;
  17. use SoftDeletes;
  18. protected $table = 'course_chapters';
  19. /**
  20. * The attributes that are mass assignable.
  21. *
  22. * @var array
  23. */
  24. protected $guarded = [];
  25. public function videos()
  26. {
  27. return $this->hasMany(Video::class, 'course_chapter_id', 'id');
  28. }
  29. public function course()
  30. {
  31. return $this->belongsTo(Course::class);
  32. }
  33. /**
  34. * 获取目录
  35. * @param $id
  36. * @return array
  37. */
  38. public static function byCourseIdGetDirectory($course_id)
  39. {
  40. $directory = [];
  41. $data = self::query()->where('course_id', $course_id)->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->get();
  42. foreach ($data as $d) {
  43. $directory[] = [
  44. 'id' => $d['id'],
  45. 'title' => $d['title'],
  46. 'children' => $d->videos()->where('status', ModelStatusEnum::OK)->where('published_at', '<', Carbon::now()->toDateTimeString())->orderByDesc('sort')->orderByDesc('published_at')->get(['id', 'title', 'duration', 'short_description'])->append(['duration_text', "progress"])
  47. ];
  48. }
  49. return $directory;
  50. }
  51. }