<?php
//sort method:
public function mostPopular()
$serials = Serial::with(['series.episodes.video' => function ($query) {
$query->withCount(['videoViews' => function($query) {
}])->orderBy('video_views_count', 'desc');
}])->get();
return $serials;
//Serial model:
public function series()
return $this->hasMany(Series::class);
//Series model:
public function episodes()
return $this->hasMany(Episode::class);
public function serial()
return $this->belongsTo(Serial::class);
//Episode model:
public function video()
return $this->hasOne(Video::class);
public function series()
return $this->belongsTo(Series::class);
//Video model:
public function videoViews()
return $this->hasMany(VideoView::class);
public function episode()