wanderreisen/laravel-popular

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

1.0.6 2020-10-05 12:17 UTC

This package is auto-updated.

Last update: 2024-09-23 16:01:25 UTC


README

Latest Version on Packagist MIT licensed

使用Laravel Popular Package,您可以追踪在时间范围内基于唯一点击次数的最受欢迎的Eloquent模型,然后按时间范围排序。

用法

在你的跟踪模型上使用visitable特性

use \Wanderreisen\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 wanderreisen/laravel-popular

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

'providers' => array(

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

),

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

$ php artisan migrate

我们准备好了!

测试

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

贡献

随时Pull Request!

作者

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。