Coder Social home page Coder Social logo

laravel-trends's Introduction

Total Downloads Latest Stable Version License

Introduction

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

Prerequisites

Before you install the package make sure you have queues working and running since Trends uses it to control the tendences. Refer to Laravel official documentation in order to configure queues in your project.

Installation

You may install Laravel Trends via Composer:

$ composer require hacklabsdev/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="Hacklabs\Trends\TrendsServiceProvider"

And finally, you should run your database migrations:

php artisan migrate

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 decays 0.25 of it's value. After more 30 minutes it decays 0.45 points of it's value. Finally, after another 30 minutes it decays 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 loose it's energy and decay over time.

Configuration

To configure your decaying time you can set the energy_decay parameter in config/trends.php. The decaying time is measured in hours.

Preparing your model

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

use Hacklabs\Trends\Traits\HasEnergy;
    
class Hashtag extends Model
{
    use HasEnergy;
    
    protected $appends = ['energy_amount'];
}

Usage

To add energy to your model use the following method:

$hashtag->addEnergy(1);

To get the current value:

$hashtag->energy->amount;

Examples

$hashtags = Hashtag::all();

$orderedHashtags = $hashtags->sortByDesc('energy_amount');

The above code creates a ordered list of hashtags based on trends.

License

Laravel Trends is open-sourced software licensed under the MIT license.

laravel-trends's People

Contributors

hacklabsdev avatar jbaron-mx avatar wsenjer avatar

Stargazers

Roman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.