Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional scopes like trending, period scopes, etc. #62

Open
francoism90 opened this issue Jul 11, 2021 · 3 comments
Open

Additional scopes like trending, period scopes, etc. #62

francoism90 opened this issue Jul 11, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@francoism90
Copy link
Contributor

It would be cool to add additional scopes, the popular scope already exists, however it doesn't have features like getting trending objects and/or objects popular in a certain period.

I'll do my best to make a PR for this to include the scopes based on the already existing popular, however I would like to ask your opinion about this one/here your ideas first. :)

@mkwsra mkwsra added the enhancement New feature or request label Jul 12, 2021
@gowl
Copy link

gowl commented Jul 12, 2021

Sounds like a fantastic idea. We had it under our radar but decided to keep it simple and go without it but feel free to have a go and if it all goes well, we'll add it 😊

I presume the query will fetch items that got a sudden burst of interactions over a short period since creation, correct?

@francoism90 francoism90 reopened this Jul 12, 2021
@francoism90
Copy link
Contributor Author

@gowl I'm not an expert and still testing, this is my solution for trending views using spatie query builder:

<?php

namespace App\Support\QueryBuilder\Sorters;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Spatie\QueryBuilder\Sorts\Sort;

class TrendingSorter implements Sort
{
    public function __invoke(Builder $query, bool $descending, string $property): Builder
    {
        return $query
            ->with('viewers')
            ->withCount(['viewers' => function (Builder $query) {
                $query->where('interactions.created_at', '>=', Carbon::now()->subDays(3));
            }])
            ->orderByDesc('viewers_count');
    }
}

@gowl
Copy link

gowl commented Jul 12, 2021

@francoism90 Looks good but it seems to me like it's another way to fetch popular posts, only this one is for the past 3 days. Optimally, a certain equation is needed that would add up views, comment count, and upvotes with different multipliers to each of these depending on importance to be authentic to the "trending" aspect of the sorter. But I believe @mkwsra has better insight than me on how to approach this the best way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants