turahe/laravel-counters

laravel系统计数器管理

v1.0.0 2024-06-26 17:21 UTC

This package is auto-updated.

Last update: 2024-09-10 09:25:44 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require Scrutinizer Code Quality PHP Composer

在某些情况下,您需要管理laravel项目中计数器的状态,例如您网站的访问者数量,或文章的查看次数,或文件的下载次数,这需要创建一个新表来保存这些记录,或者至少为您的表添加新列来保存计数值。
因此,使用这个包,您可以为您的模型创建任意数量的计数器,而无需在模型表中创建物理列。此外,您还可以存储公共计数器,例如“number_of_downloads”,而无需创建整个表来存储它。

总之,这个包允许您管理laravel项目中的计数器。

安装后,您可以执行类似以下操作

//increment/decrement system counters
Counters::increment('number_of_downloads'); 

// increment/decrement model objects counters
$post->incrementCounter('number_of_views');
$feature->decrementCounter('number_of_bugs');
$user->decrementCounter('balance', 2); // decrement the balance of the user by 2

下面提到了许多其他方法。

安装

Laravel

此包可用于Laravel 5.4或更高版本。如果您使用的是较旧版本的Laravel,您可以通过composer安装此包

composer require turahe/laravel-counters

在Laravel 5.5及以上版本中,服务提供程序将自动注册。在框架的较旧版本中,只需将服务提供程序添加到config/app.php文件中

'providers' => [
    // ...
    Turahe\Counters\CountersServiceProvider::class,
    //...
];

您必须发布以下迁移迁移

php artisan vendor:publish --provider="Turahe\Counters\CountersServiceProvider"

迁移发布后,您可以通过运行迁移来创建表

php artisan migrate

用法

1) 使用无模型的计数器

首先,将Turahe\Counters\Traits\HasCounter特性添加到您的模型(s)中:例如,我们可以将其添加到Post模型中

use Turahe\Counters\Traits\HasCounter;

class Post extends Model
{
    use HasCounter;

    // ...
}

此包允许将帖子与计数器关联。每个帖子可以关联多个计数器。

我们可以创建一个计数器,例如,让我们为Post模型创建一个查看次数计数器。

use Turahe\Counters\Models\Counter;

$counter = Counter([
            'key' => 'number_of_views',
            'name' => 'Views',
            'initial_value' => 0 //(could be left empty, default value is 0)
            'step' => 1 // (could be left empty, default value is 1)
        ]);

之后,例如,在帖子控制器的show函数中,您可以添加以下行

class PostsController extends Controller
{
    public function show(Post $post)
    {
        //...
        $post->incrementCounter('number_of_views');
        //...
    }
}

通过这样做,每当展示帖子时,每个帖子的number_of_views计数器都会增加[步长]

此包还有其他功能。

// will return the counter object
$post->getCounter($key); 

// will return the counter value
$post->getCounterValue($key);

//will add record in counterable table for this post object
$post->addCounter($key);

//will remove the record from counterable table for this post object.
$post->removeCounter($key);

//increment the counter with the given $key
//Note that this will create record in counterable table,if it's not exist
//if $step is entered, it will increment with the value of $step
$post->incrementCounter($key, $step = null);

//decrement the counter with the given $key
//Note that this will create record in counterable table,if it's not exist
//if $step is entered, it will decrement with the value of $step
$post->decrementCounter($key, $step = null);

 // will reset the counter value (to initial_value) for the post object.
$post->resetCounter($key);

2) 使用无模型的计数器。

有时,您有与任何模型无关的通用计数器,例如您网站的访问者数量。

因此,此包将允许您处理这些类型的计数器。

use Turahe\Counters\Facades\Counters; 
class Test 
{
    public function incrementFunction()
    {
        //moreover, you can add this function in your public page to be incremented 
        //every time user hits your website
        Counters::increment('number_of_downloads');
    }
}

此外观有其他许多功能

// will return the counter object
Counters::get($key); 

// will return the counter value
Counters::getValue($key, $default = null); 

// set the value of the counter
Counters::setValue($key, $value);

//set the step of the counter
Counters::setStep($key, $step);

//increment the counter with the given $key
Counters::increment($key, $step = null);

//decrement the counter with the given $key
Counters::decrement($key, $step = null);

 // will reset the counter for the initial_value
Counters::reset($key);

在某些情况下,您希望为每个人增加一次计数,例如,不需要每次相同用户刷新页面时都增加number_of_downloads计数器的值。

因此,您可以使用这些函数

Counters::incrementIfNotHasCookies($key);
Counters::decrementIfNotHasCookies($key);

3) 使用artisan命令

您可以使用artisan命令从控制台创建计数器。以下命令创建初始值为0,步长为1的计数器number_of_downloads

php artisan make:counter number_of_downloads Visitors 0 1

数据库初始化

以下是一个示例seeder。

    use Illuminate\Database\Seeder;
    use Turahe\Counters\Facades\Counters;

    class CounterTableSeeder extends Seeder
    {
        public function run()
        {

            // create Counters
            //This will create a counter with initial value as 3, and every increment 5 will be added.
            Counter::create([
                'key' => 'number_of_downloads',
                'name' => 'Visitors',
                'initial_value' => 3,
                'step' => 5
            ]);
            //This counter will have 0 as initial_value and 1 as step
            Counter::create([
                'key' => 'number_of_downloads2',
                'name' => 'Visitors2'
            ]);

            $viewCounter = Counter::create([
                'key' => 'number_of_views',
                'name' => 'Views'
            ]);
            
            $post = Post::find(1);
            $post->addCounter('number_of_views');// to add the record to counterable table
            
            
        }
    }