isGuest()) { return collect(); } $query = $this->permissions ->restrictEntityRelationQuery(Favourite::query(), 'favourites', 'favouritable_id', 'favouritable_type') ->select('favourites.*') ->leftJoin('views', function (JoinClause $join) { $join->on('favourites.favouritable_id', '=', 'views.viewable_id'); $join->on('favourites.favouritable_type', '=', 'views.viewable_type'); $join->where('views.user_id', '=', user()->id); }) ->orderBy('views.views', 'desc') ->where('favourites.user_id', '=', user()->id); $favourites = $query ->skip($skip) ->take($count) ->get(); $this->listLoader->loadIntoRelations($favourites->all(), 'favouritable', false); return $favourites->pluck('favouritable')->filter(); } }