123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Repositories\Models\Course;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Model;
- use App\Repositories\Models\Base\Resource;
- use App\Repositories\Models\User\User;
- use App\Repositories\Models\UserVideoWatchRecord;
- use Prettus\Repository\Contracts\Transformable;
- use Prettus\Repository\Traits\TransformableTrait;
- /**
- * Class Course.
- *
- * @package namespace App\Repositories\Models;
- */
- class Course extends Model implements Transformable
- {
- use TransformableTrait;
- protected $table = 'course_courses';
- protected $appends = ['video_nums'];
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
- protected static function booted()
- {
- self::language();
- }
- public function user()
- {
- return $this->belongsTo(User::class)->select(['id', 'name', 'turename', 'mobile', 'headimg', 'userrate', 'personal_signature']);
- }
- public function category()
- {
- return $this->belongsTo(Category::class, 'category_id', 'id')->select(['id', 'name']);
- }
- public function thumb_resource()
- {
- return $this->belongsTo(Resource::class, 'thumb', 'id')->select(['path', 'id', 'url']);
- }
- public function attaches()
- {
- return $this->hasMany(Attach::class, 'course_id', 'id');
- }
- public function subscribe()
- {
- return $this->hasOne(UserRecord::class);
- }
- public function comments()
- {
- return $this->hasMany(Comment::class);
- }
- public function video_watch_record()
- {
- return $this->hasMany(UserVideoWatchRecord::class, 'course_id', 'id');
- }
- public function getVideoNumsAttribute()
- {
- return Video::query()->where('course_id', $this->attributes['id'])->where('status', ModelStatusEnum::OK)->count();
- }
- public function getWatchProgressAttribute()
- {
- return UserRecord::query()->where('course_id', $this->attributes['id'])->where('user_id', login_user_id())->value('progress') ?? false;
- }
- public function organizations()
- {
- return $this->hasMany(OrganizationCourse::class);
- }
- public function collections()
- {
- return $this->hasMany(Collection::class);
- }
- public function getIsCollectionStatusAttribute()
- {
- return Collection::query()->where('course_id', $this->attributes['id'])->where('user_id', login_user_id())->status()->exists();
- }
- }
|