isDefault()) { return collect(); } $query = $this->permissionService() ->filterRestrictedEntityRelations(View::query(), 'views', 'viewable_id', 'viewable_type', 'view') ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count')) ->groupBy('viewable_id', 'viewable_type') ->rightJoin('favourites', function (JoinClause $join) { $join->on('views.viewable_id', '=', 'favourites.favouritable_id'); $join->on('views.viewable_type', '=', 'favourites.favouritable_type'); $join->where('favourites.user_id', '=', user()->id); }) ->orderBy('view_count', 'desc'); return $query->with('viewable') ->skip($count * ($page - 1)) ->take($count) ->get() ->pluck('viewable') ->filter(); } }