PHPackages                             hawaaworld/laravel-trends - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hawaaworld/laravel-trends

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hawaaworld/laravel-trends
=========================

Laravel Trends provides a lightweight trending system to your application.

v2.1.0(1y ago)199MITPHPPHP ^8.0|^8.1|^8.2|^8.3

Since Mar 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/hawaaworld/laravel-trends)[ Packagist](https://packagist.org/packages/hawaaworld/laravel-trends)[ RSS](/packages/hawaaworld-laravel-trends/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (2)Versions (18)Used By (0)

[![Laravel Trends](https://camo.githubusercontent.com/fd80d28d4ae6316ee754e2a1d0c8e1282cf87bc0ff25b4e58fbe65a19b85543f/68747470733a2f2f692e6962622e636f2f636363663734742f6c6f676f2e706e67)](https://camo.githubusercontent.com/fd80d28d4ae6316ee754e2a1d0c8e1282cf87bc0ff25b4e58fbe65a19b85543f/68747470733a2f2f692e6962622e636f2f636363663734742f6c6f676f2e706e67)

[![Total Downloads](https://camo.githubusercontent.com/39ddec39a4de7a84c342e2083c56a5bb9ff2f034bb363db547d90e817e891a8b/68747470733a2f2f706f7365722e707567782e6f72672f6861776161776f726c642f6c61726176656c2d7472656e64732f642f746f74616c)](https://packagist.org/packages/hawaaworld/laravel-trends) [![Latest Stable Version](https://camo.githubusercontent.com/115edbc0d20e5a98d1fa27b505699b3de036d1b288c99282cedfe3a0568e8f09/68747470733a2f2f706f7365722e707567782e6f72672f6861776161776f726c642f6c61726176656c2d7472656e64732f762f737461626c65)](https://packagist.org/packages/hawaaworld/laravel-trends) [![License](https://camo.githubusercontent.com/5a831b4a65eeb2ac40cbd41f3da96a59cd1f13906924a04f487be3bf73177429/68747470733a2f2f706f7365722e707567782e6f72672f6861776161776f726c642f6c61726176656c2d7472656e64732f6c6963656e7365)](https://packagist.org/packages/hawaaworld/laravel-trends)

> **Note**This package is a fork of [hacklabsdev/laravel-trends](https://github.com/hacklabsdev/laravel-trends) and is not maintained by the original author.

Introduction
------------

[](#introduction)

Have you ever wondered how Twitter's Trending Topics works? Laravel Trends provides a lightweight trending system to your application.

Prerequisites
-------------

[](#prerequisites)

Before you install the package make sure you have queues working and running since Trends uses it to control the tendencies. Refer to Laravel [official documentation](https://laravel.com/docs/master/queues#introduction "official documentation") in order to configure queues in your project.

Installation
------------

[](#installation)

You may install Laravel Trends via Composer:

`composer require hawaaworld/laravel-trends`

Next, publish the Trends configuration and migration files using the vendor:publish command. The configuration file will be placed in your config directory:

`php artisan vendor:publish --provider="Hawaaworld\Trends\TrendsServiceProvider"`

And finally, you should run your database migrations:

`php artisan migrate`

How it works
------------

[](#how-it-works)

`Trends` allows you to create a trending system for any model you want. Let's take Twitter as an example. Everytime a hashtag is tweeted it receives 1 point of energy, but after 30 minutes this single point of energy loss 0.25 of its value. After more 30 minutes it loss 0.45 points of its value. Finally, after another 30 minutes it loss 0.30 of its value returning to 0. But how can a trend be detected? Imagine that thousands of people hits the same hashtag at the same time, this hashtag will have thousands of energy points and if you have an ordered list of hashtags this one will surely be on top, but after a few minutes if this hashtag doesn't receive any more energy points it will start to lose its energy and loss over time.

Configuration
-------------

[](#configuration)

To configure your losing time you can set the `loss_time` parameter in `config/trends.php`. The losing time is measured in hours.

Preparing your model
--------------------

[](#preparing-your-model)

To allow your model to work with Trends you'll need to implement the Energy interface with HasEnergy trait. And in order to return the current model's energy value, add `energy_amount` to your serialization.

```
use Hawaaworld\Trends\Contracts\Energy;
use Hawaaworld\Trends\Traits\HasEnergy;

class Hashtag extends Model implements Energy
{
    use HasEnergy;

    protected $appends = ['energy_amount'];
}
```

Usage
-----

[](#usage)

To add energy to your model use the following method:

```
$hashtag->addEnergy(amount: 1.0);
```

To get the current energy amount:

```
$hashtag->energy->amount;
```

Getting the top trends
----------------------

[](#getting-the-top-trends)

to get the top trends from all types of models use the following method:

```
$trending = Trends::top(10);

/**
 * returns a collection type of the top 10 trending models:
 *
 * Illuminate\Support\Collection {
 *   #items: array:10 [
 *     0 => App\Models\Article
 *     1 => App\Models\Article
 *     2 => App\Models\Video
 *     3 => App\Models\Article
 *     4 => App\Models\Hashtag
 *     5 => App\Models\Hashtag
 *     6 => App\Models\Article
 *     7 => App\Models\Video
 *     8 => App\Models\Comment
 *     9 => App\Models\Comment
 *   ]
 * }
 */
```

To customize the model you want to retrieve, let's say you want to get the top 10 trending videos, you can use the following method:

```
$trendingVideos = Trends::top(10, Video::class);
```

You can set your own query builder to get the trending models you want:

```
$trendingShortVideos = Trends::top(10, Video::class, function($query) {
    $query->where('duration', '
