Video.php 1.8 KB

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