zepson/laravel-popular

一个用于跟踪网站在特定时间点条目(模型)流行度的Laravel包。

8.1 2021-02-21 18:22 UTC

This package is auto-updated.

Last update: 2024-09-22 02:16:47 UTC


README

Latest Version on Packagist MIT licensed

使用Laravel Popular包,您可以跟踪基于时间段内唯一点击次数的Eloquent模型流行度,并在时间段内按流行度排序。

用法

在您打算跟踪的模型上使用visitable特性

use \JordanMiguel\LaravelPopular\Traits\Visitable;

class Post extends Model
{
    use Visitable;

    ...
}

以下是一些代码示例

// Adding a visit to the post. Recommended on the show() method of your controller.
$post->visit();

// Retrieving the count of visitors in a timeframe
$post->visitsDay();
$post->visitsWeek();
$post->visitsMonth();
$post->visitsForever();

// Ordering the posts by the most visited
Posts::popularLast(3)->get(); // Get popular posts on the last 3 days

Posts::popularDay()->get(); // Get posts ordered by the most visited on the last 24h
Posts::popularWeek()->get();
Posts::popularMonth()->get();
Posts::popularYear()->get();
Posts::popularAllTime()->get();

安装

通过Composer

$ composer require jordanmiguel/laravel-popular

如果您使用的是Laravel <= 5.4,请将'JordanMiguel\LaravelPopular\LaravelPopularServiceProvider::class',添加到您的config/app.php文件中,放置在$providers数组末尾

'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'JordanMiguel\LaravelPopular\LaravelPopularServiceProvider::class',

),

现在,让我们在数据库中创建我们的表

$ php artisan migrate

我们准备好了!

测试

目前还没有测试设置,如果您做了,请发起pull request =)

贡献

随时可以发起Pull Request!

作者

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件