lahatre/laravel-money 问题修复 & 功能扩展

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

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

lahatre/laravel-money

Composer 安装命令:

composer require lahatre/laravel-money

包简介

A precise, immutable, and float-safe money value object for Laravel.

README 文档

README

This package provides a precise and developer-friendly way to handle monetary values in Laravel.

It solves the common pitfalls of floating-point arithmetic (like 0.1 + 0.2 !== 0.3) by using BCMath and integer storage under the hood. It offers a fluent API to perform calculations, handle rounding, and format prices, all while seamlessly integrating with Eloquent.

Installation

You can install the package via composer:

composer require lahatre/money

Usage

Preparing the database

We recommend storing money values as integers (minor units, e.g., cents).

Schema::create('products', function (Blueprint $table) {
    $table->id();
    // 10.00 is stored as 1000
    $table->bigInteger('price')->default(0); 
    $table->timestamps();
});

Preparing the model

Add the MoneyCast to your Eloquent model. This will automatically convert the integer from the database into a Money object instance.

use Illuminate\Database\Eloquent\Model;
use Lahatre\Money\Casts\MoneyCast;

class Product extends Model
{
    protected $casts = [
        'price' => MoneyCast::class,
    ];
}

working with Money

You can assign values using strings, integers, or floats. The package will normalize them for you.

$product = new Product();

// You can assign a string (recommended for precision)
$product->price = '19.99'; 

// Or a float
$product->price = 19.99;

// The value is saved as an integer (1999)
$product->save(); 

When retrieving the value, you get an immutable Money instance.

// Automatic formatting as string
echo $product->price; // "19.99"

// Fluent arithmetic
$newPrice = $product->price->add('5.00'); // "24.99"

Calculations & Rounding

All operations return a new instance of Money. By default, the package uses Banker's Rounding (HALF_UP) and 2 decimals precision.

$price = Money::from('100.00');

// Chaining
$total = $price
    ->add('50.00')
    ->mul('0.20'); // Returns a new Money instance

// Splitting values (e.g. 10 / 3)
// You can define a specific rounding mode for operations that require it
use Lahatre\Money\Support\BigNumber;

$bill = Money::from('10.00');

$split = $bill->div(3, BigNumber::ROUND_UP); // "3.34"

Configuration

You can publish the config file to change the default precision (e.g. for cryptocurrencies) or the global rounding mode.

php artisan vendor:publish --tag=money-config

lahatre/laravel-money 适用场景与选型建议

lahatre/laravel-money 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lahatre/laravel-money 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lahatre/laravel-money 我们能提供哪些服务?
定制开发 / 二次开发

基于 lahatre/laravel-money 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2026-01-12