testmonitor/eloquent-calculated-columns 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

testmonitor/eloquent-calculated-columns

最新稳定版本:v2.0.0

Composer 安装命令:

composer require testmonitor/eloquent-calculated-columns

包简介

A Laravel package for adding calculated columns to Eloquent models using SQL for more performant queries.

README 文档

README

Latest Stable Version codecov StyleCI License

A Laravel package for adding calculated columns when retrieving data from an Eloquent model. This package allows you to define these columns using SQL, resulting in more performant queries compared to accessors.

It is heavily inspired by Spatie's Query Builder and can be used in conjunction with this package.

Table of Contents

Installation

This package can be installed through Composer:

$ composer require testmonitor/eloquent-calculated-columns

Next, publish the configuration file:

$ php artisan vendor:publish --tag=eloquent-calculated-columns

The configuration file allows you the change the HTTP parameter name, when desired.

Usage

To use calculated columns, you need to:

  1. Use the trait TestMonitor\CalculatedColumns\HasCalculatedColumns in your model.
  2. Define the available calculated in your model.

Add the CalculatedColumns trait to the models where you want to add calculated columns:

use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;
use TestMonitor\CalculatedColumns\HasCalculatedColumns;

class User extends Model
{
    use HasCalculatedColumns;

    public function calculatedColumns(): array
    {
        return [
            'total_price' => function (Builder $query) {
                $query->select(DB::raw("SUM(order_items.price) AS total_price"))
                    ->from('order_items')
                    ->whereColumn('order_items.order_id', 'orders.id');
            },
        ];
    }
}

Next, use the calculated columns in your queries:

use App\Models\Order;

$orders = Order::query()
    ->withCalculatedColumns()
    ->get();
}

In this example, the total_price column calculates the total price of an order by adding all the order item prices.

The requested columns are automatically derived from the HTTP request. You can modify the HTTP query parameter in the configuration file. By default, the name calculate is used.

Examples

Tests

The package contains integration tests. You can run them using PHPUnit.

$ vendor/bin/phpunit

Changelog

Refer to CHANGELOG for more information.

Contributing

Refer to CONTRIBUTING for contributing details.

Credits

License

The MIT License (MIT). Refer to the License for more information.

统计信息

  • 总下载量: 669
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-09