korridor/laravel-computed-attributes

Laravel 包,为 Eloquent 模型添加计算属性。计算属性是一种访问器,其中计算值被保存到数据库中。

3.1.0 2024-03-01 14:15 UTC

This package is auto-updated.

Last update: 2024-09-06 12:34:53 UTC


README

Latest Version on Packagist License Supported PHP versions GitHub Workflow Lint GitHub Workflow Tests Codecov

Laravel 包,为 Eloquent 模型添加计算属性。计算属性是一种访问器,其中的值被保存到数据库中。该值可以在任何时间重新生成或验证。这可以提高性能(无需在每次获取/检索时进行计算)并且可以简化数据库查询(例如,复杂的筛选系统)。

注意

查看 solidtime - 现代开源时间追踪器,链接:[solidtime.io](https://www.solidtime.io)

安装

您可以使用以下命令通过 composer 安装此包:

composer require korridor/laravel-computed-attributes

如果您想使用此包与较旧的 Laravel/PHP 版本,请安装 2.2.* 版本。

composer require korridor/laravel-computed-attributes "^2.2"

您还可以发布配置文件以更改默认配置(例如,模型文件夹路径)。

php artisan vendor:publish --tag=computed-attributes-config

需求

此包已针对以下 Laravel 和 PHP 版本进行测试

  • 10.* (PHP 8.1, 8.2, 8.3)
  • 11.* (PHP 8.2, 8.3)

使用示例

以下是一个两个计算属性 complex_calculationsum_of_votes 的示例。函数 getComplexCalculationComputedgetSumOfVotesComputed 用于计算计算属性。

use Korridor\LaravelComputedAttributes\ComputedAttributes;

class Post {

    use ComputedAttributes;

    /**
     * The attributes that are computed. (f.e. for performance reasons)
     * These attributes can be regenerated at any time.
     *
     * @var string[]
     */
    protected $computed = [
        'complex_calculation',
        'sum_of_votes',
    ];

    /*
     * Computed attributes.
     */

    /**
     * @return int
     */
    public function getComplexCalculationComputed(): int
    {
        return 1 + 2;
    }

    /**
     * @return int
     */
    public function getSumOfVotesComputed(): int
    {
        return $this->votes->sum('rating');
    }
    
    // ...
}

https://laravel.net.cn/docs/8.x/eloquent#events

/**
 * Boot function from laravel.
 */
protected static function boot(): void
{
    static::saving(function (Post $model) {
        $model->setComputedAttributeValue('sum_of_votes');
    });
    parent::boot();
}

要查看此非常简单示例的完整代码,请参阅 tests/TestEnvironment 文件夹。

命令

computed-attributes:generate

computed-attributes:generate { modelsAttributes? } { --chunkSize=500 } { --chunk= }

此命令(重新)计算计算属性的值并保存新值。

查询优化

您可以使用具有 ComputedAttributes 特性的 computedAttributesGenerate 范围在任何模型中扩展用于计算的模型检索查询。

use Illuminate\Database\Eloquent\Builder;

// ...

/**
 * This scope will be applied during the computed property generation with artisan computed-attributes:generate.
 *
 * @param Builder $builder
 * @param array $attributes Attributes that will be generated.
 * @return Builder
 */
public function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder
{
    if (in_array('sum_of_votes', $attributes)) {
        return $builder->with('votes');
    }

    return $builder;
}

computed-attributes:validate

artisan computed-attributes:validate { modelsAttributes? } { --chunkSize=500 } { --chunk= }

此命令验证计算属性的当前值。

查询优化

use Illuminate\Database\Eloquent\Builder;

// ...

/**
 * This scope will be applied during the computed property validation with artisan computed-attributes:validate.
 *
 * @param Builder $builder
 * @param array $attributes Attributes that will be validated.
 * @return Builder
 */
public function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder
{
    if (in_array('sum_of_votes', $attributes)) {
        return $builder->with('votes');
    }

    return $builder;
}

贡献

我欢迎建议和贡献。只需创建一个问题或拉取请求。

本地 Docker 环境

docker 文件夹包含用于开发的本地 Docker 环境。Docker 工作区已安装 composer 和 xdebug。

docker-compose run workspace bash

测试

composer test 命令使用 phpunit 运行所有测试。使用 composer test-coverage 命令使用 phpunit 运行所有测试并在 coverage 文件夹中创建覆盖率报告。

代码格式化/代码审查

composer fix 命令使用 php-cs-fixer 格式化代码。使用 composer lint 命令使用 phpcs 检查代码。

许可协议

此包根据 MIT 许可协议(MIT)授权。有关更多信息,请参阅 许可文件