Video.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. protected static function booted()
  18. {
  19. self::language();
  20. }
  21. /**
  22. * The attributes that are mass assignable.
  23. *
  24. * @var array
  25. */
  26. // protected $fillable = [];
  27. protected $guarded = [];
  28. public function getDurationTextAttribute()
  29. {
  30. return sec2time($this->attributes['duration']);
  31. }
  32. public function comments()
  33. {
  34. return $this->hasMany(Comment::class, 'course_video_id');
  35. }
  36. public function attaches()
  37. {
  38. return $this->hasMany(Attach::class, 'course_video_id');
  39. }
  40. public function course_chapter()
  41. {
  42. return $this->belongsTo(Chapter::class);
  43. }
  44. public function course()
  45. {
  46. return $this->belongsTo(Course::class);
  47. }
  48. public function getProgressAttribute()
  49. {
  50. return UserVideoWatchRecord::query()->where('video_id', $this->attributes['id'])->where('user_id', login_user_id())->value('progress') ?? 0;
  51. }
  52. public function url_resource()
  53. {
  54. return $this->belongsTo(Resource::class, 'url', 'id')->select(['path', 'id', 'url']);
  55. }
  56. public function subtitle_zh_path_resource()
  57. {
  58. return $this->belongsTo(Resource::class, 'subtitle_zh_path', 'id')->select(['path', 'id', 'url']);
  59. }
  60. public function subtitle_en_path_resource()
  61. {
  62. return $this->belongsTo(Resource::class, 'subtitle_en_path', 'id')->select(['path', 'id', 'url']);
  63. }
  64. }