Open francoism90 opened 3 years ago
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?
@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');
}
}
@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.
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. :)