Course.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Repositories\Models\Course;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Model;
  5. use App\Repositories\Models\Base\Resource;
  6. use App\Repositories\Models\User\User;
  7. use App\Repositories\Models\UserVideoWatchRecord;
  8. use Prettus\Repository\Contracts\Transformable;
  9. use Prettus\Repository\Traits\TransformableTrait;
  10. /**
  11. * Class Course.
  12. *
  13. * @package namespace App\Repositories\Models;
  14. */
  15. class Course extends Model implements Transformable
  16. {
  17. use TransformableTrait;
  18. protected $table = 'course_courses';
  19. protected $appends = ['video_nums'];
  20. /**
  21. * The attributes that are mass assignable.
  22. *
  23. * @var array
  24. */
  25. protected $guarded = [];
  26. protected static function booted()
  27. {
  28. self::language();
  29. }
  30. public function user()
  31. {
  32. return $this->belongsTo(User::class)->select(['id', 'name', 'turename', 'mobile', 'headimg', 'userrate', 'personal_signature']);
  33. }
  34. public function category()
  35. {
  36. return $this->belongsTo(Category::class, 'category_id', 'id')->select(['id', 'name']);
  37. }
  38. public function thumb_resource()
  39. {
  40. return $this->belongsTo(Resource::class, 'thumb', 'id')->select(['path', 'id', 'url']);
  41. }
  42. public function attaches()
  43. {
  44. return $this->hasMany(Attach::class, 'course_id', 'id');
  45. }
  46. public function subscribe()
  47. {
  48. return $this->hasOne(UserRecord::class);
  49. }
  50. public function comments()
  51. {
  52. return $this->hasMany(Comment::class);
  53. }
  54. public function video_watch_record()
  55. {
  56. return $this->hasMany(UserVideoWatchRecord::class, 'course_id', 'id');
  57. }
  58. public function getVideoNumsAttribute()
  59. {
  60. return Video::query()->where('course_id', $this->attributes['id'])->where('status', ModelStatusEnum::OK)->count();
  61. }
  62. public function getWatchProgressAttribute()
  63. {
  64. return UserRecord::query()->where('course_id', $this->attributes['id'])->where('user_id', login_user_id())->value('progress') ?? false;
  65. }
  66. public function organizations()
  67. {
  68. return $this->hasMany(OrganizationCourse::class);
  69. }
  70. public function collections()
  71. {
  72. return $this->hasMany(Collection::class);
  73. }
  74. public function getIsCollectionStatusAttribute()
  75. {
  76. return Collection::query()->where('course_id', $this->attributes['id'])->where('user_id', login_user_id())->status()->exists();
  77. }
  78. }