Advertisement
Ostap34PHP

Untitled

Mar 25th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\DB;
  7. use Laravelista\Comments\Commentable;
  8.  
  9. class Course extends Model
  10. {
  11.     use Commentable;
  12.  
  13.     protected $fillable = ['id', 'title', 'name', 'description', 'description_short', 'description_after', 'image'];
  14.  
  15.     public function sections()
  16.     {
  17.         return $this->hasMany('App\Section');
  18.     }
  19.  
  20.     public function scopeWithQuestionsCount($query)
  21.     {
  22.         $query->selectSub(function ($query) {
  23.             $query->from('questions')
  24.                 ->selectRaw('COUNT(id)')
  25.                 ->whereIn('lesson_id', function ($query) {
  26.                     $query->select('id')->from('lessons')->whereIn('section_id', function ($query) {
  27.                         $query->select('id')->from('sections')->whereRaw('`sections`.`course_id` = `courses`.`id`');
  28.                     });
  29.                 });
  30.         }, 'questions_count');
  31.     }
  32.  
  33.     public function scopeWithExercisesCount($query)
  34.     {
  35.         $query->selectSub(function ($query) {
  36.             $query->from('exercises')
  37.                 ->selectRaw('COUNT(id)')
  38.                 ->whereIn('lesson_id', function ($query) {
  39.                     $query->select('id')->from('lessons')->whereIn('section_id', function ($query) {
  40.                         $query->select('id')->from('sections')->whereRaw('`sections`.`course_id` = `courses`.`id`');
  41.                     });
  42.                 });
  43.         }, 'exercises_count');
  44.     }
  45.  
  46.     public function scopeWithLessonsCount($query)
  47.     {
  48.         $lessons_count = Lesson::selectRaw('COUNT(id)')->whereIn('section_id', function ($query) {
  49.             $query->select('id')->from('sections')->whereRaw('`sections`.`course_id` = `courses`.`id`');
  50.         });
  51.         return $query->selectSub($lessons_count, 'lessons_count');
  52.     }
  53.  
  54.     public function scopeWithCompletedLessonsCount($query)
  55.     {
  56.         $completed_lessons_count =
  57.             DB::table('lesson_user')->selectRaw('COUNT(lesson_id)')->whereIn('lesson_id', function ($query) {
  58.                 $query->select('id')->from('lessons')->whereIn('section_id', function ($query) {
  59.                     $query->select('id')->from('sections')->whereRaw('`sections`.`course_id` = `courses`.`id`');
  60.                 });
  61.             });
  62.         $query->selectSub($completed_lessons_count, 'completed_lessons_count');
  63.     }
  64.  
  65.     public function scopeWithLastCompletedLessonId($query)
  66.     {
  67.         $last_completed_lesson_id = DB::table('lesson_user')->select('lesson_id')->whereIn('lesson_id', function ($query) {
  68.             $query->select('id')->from('lessons')->whereIn('section_id', function ($query) {
  69.                 $query->select('id')->from('sections')->whereRaw('`sections`.`course_id` = `courses`.`id`');
  70.             });
  71.         })->orderBy('lesson_id', 'desc')->limit(1);
  72.         return $query->selectSub($last_completed_lesson_id, 'last_completed_lesson_id');
  73.     }
  74.  
  75.     public static function boot()
  76.     {
  77.         parent::boot();
  78.  
  79.         static::creating(function ($table) {
  80.             $table->user_id = auth()->id();
  81.         });
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement