Video.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Repositories\Models\Course;
  3. use App\Repositories\Models\Model;
  4. use App\Repositories\Models\Base\Resource;
  5. use Prettus\Repository\Contracts\Transformable;
  6. use Prettus\Repository\Traits\TransformableTrait;
  7. /**
  8. * Class CourseVideo.
  9. *
  10. * @package namespace App\Repositories\Models;
  11. */
  12. class Video extends Model implements Transformable
  13. {
  14. use TransformableTrait;
  15. protected $table = 'course_videos';
  16. protected $guarded = [];
  17. public function getDurationTextAttribute()
  18. {
  19. return sec2time($this->attributes['duration']);
  20. }
  21. public function comments()
  22. {
  23. return $this->hasMany(Comment::class, 'course_video_id');
  24. }
  25. public function attaches()
  26. {
  27. return $this->hasMany(Attach::class, 'course_video_id');
  28. }
  29. public function course_chapter()
  30. {
  31. return $this->belongsTo(Chapter::class);
  32. }
  33. public function course()
  34. {
  35. return $this->belongsTo(Course::class)->select(['id', 'title']);
  36. }
  37. public function getProgressAttribute()
  38. {
  39. return WatchRecord::query()->where('video_id', $this->attributes['id'])->where('user_id', login_user_id())->orderByDesc('progress_index')->value('progress') ?? 0;
  40. }
  41. public function url_resource()
  42. {
  43. return $this->belongsTo(Resource::class, 'url', 'id')->select(['path', 'id', 'url']);
  44. }
  45. public function subtitle_zh_path_resource()
  46. {
  47. return $this->belongsTo(Resource::class, 'subtitle_zh_path', 'id')->select(['path', 'id', 'url']);
  48. }
  49. public function subtitle_en_path_resource()
  50. {
  51. return $this->belongsTo(Resource::class, 'subtitle_en_path', 'id')->select(['path', 'id', 'url']);
  52. }
  53. }