Course.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 Prettus\Repository\Contracts\Transformable;
  7. use Prettus\Repository\Traits\TransformableTrait;
  8. /**
  9. * Class Course.
  10. *
  11. * @package namespace App\Repositories\Models;
  12. */
  13. class Course extends Model implements Transformable
  14. {
  15. use TransformableTrait;
  16. protected $table = 'course_courses';
  17. protected $guarded = [];
  18. protected $casts = [
  19. 'settings' => 'json',
  20. 'labels' => 'json',
  21. ];
  22. public function category()
  23. {
  24. return $this->belongsTo(Category::class, 'category_id', 'id')->select(['name', 'id']);
  25. }
  26. public function thumb_resource()
  27. {
  28. return $this->belongsTo(Resource::class, 'thumb', 'id')->select(['path', 'id', 'url']);
  29. }
  30. public function attaches()
  31. {
  32. return $this->hasMany(Attach::class, 'course_id', 'id');
  33. }
  34. public function subscribe()
  35. {
  36. return $this->hasOne(Subscribe::class)->select(['id', 'is_watched', 'progress', 'watched_at', 'created_at']);
  37. }
  38. public function comments()
  39. {
  40. return $this->hasMany(Comment::class);
  41. }
  42. public function getVideoNumsAttribute()
  43. {
  44. return Video::query()->where('course_id', $this->attributes['id'])->where('status', ModelStatusEnum::OK)->count();
  45. }
  46. }