wamesk/laravel-nova-price-field 问题修复 & 功能扩展

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

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

wamesk/laravel-nova-price-field

Composer 安装命令:

composer require wamesk/laravel-nova-price-field

包简介

A Laravel Nova price field with moneyphp/money library.

关键字:

README 文档

README

Installation

composer require wamesk/laravel-nova-price-field

Casts

This package provides two Eloquent casts for storing prices as integers (cents).

PriceCast

Full-featured cast with tax support. Use when the model has tax logic (withTax, withoutTax, taxAmount, etc.).

use Wame\LaravelNovaPriceField\Casts\PriceCast;

protected function casts(): array
{
    return [
        'price' => PriceCast::class,
    ];
}

Available methods on the cast value:

Method Description
asFloat() Price as float (cents ÷ 100)
asMoney() Money object
formatted() Formatted string (e.g. 1 234,50 EUR)
withTax($formatted) Money object or formatted string
withoutTax($formatted) Money object or formatted string (requires tax config)
tax($formatted) Tax rate as int or formatted string
taxAmount($formatted) Tax amount as Money or formatted string
totalWithTax($formatted) Total incl. tax × quantity
totalWithoutTax($formatted) Total excl. tax × quantity
totalTaxAmount($formatted) Total tax amount × quantity

Optional model static properties:

// Automatically reads price_without_tax column
public static string|bool $priceWithoutTaxColumn = true;

// Custom tax column (default: 'tax')
public static string $taxColumn = 'vat';

// Custom quantity column (default: 'quantity')
public static string $quantityColumn = 'qty';

// Custom currency column
public static string $currencyColumn = 'currency_code';

SimplePriceCast

Lightweight cast without any tax logic. Use for cost fields, budgets, or any price stored without VAT context.

use Wame\LaravelNovaPriceField\Casts\SimplePriceCast;

protected function casts(): array
{
    return [
        'planned_cost' => SimplePriceCast::class,
        'actual_cost'  => SimplePriceCast::class,
    ];
}

Available methods on the cast value:

Method Description
asFloat() Price as float (cents ÷ 100)
asMoney() Money object
formatted() Formatted string (e.g. 1 234,50 EUR)

Both casts share the same base class (AbstractPriceCast) and resolve the currency from the model's currency, currency_id, or $currencyColumn property (defaulting to EUR).

Nova Field

Auto-detection of cast type

The Price field automatically detects which cast is used on the model attribute and adjusts its behaviour accordingly — no manual configuration needed.

Cast Behaviour
PriceCast Full tax UI: shows withTax, withoutTax, tax amount, tax percentage
SimplePriceCast Tax UI disabled automatically; shows only the formatted price

If ->withAllFieldOnForm() is called on a field backed by SimplePriceCast, the tax input sub-fields are suppressed automatically (the with_all_field_on_form meta is overridden to false at resolve time).

// PriceCast — full tax fields shown automatically
Price::make('Price', 'price'),

// SimplePriceCast — tax fields hidden automatically, even with withAllFieldOnForm()
Price::make('Planned cost', 'planned_cost'),

Shows a single input for price. When viewing on detail or index, shows price with currency suffix.

use Wame\LaravelNovaPriceField\Fields\Price;

Price::make('price'),

Showing total price

Price::make('price')
    ->asTotal(),

Usage should be only for viewing (detail and index page).

Multiple fields on form

Adds price without tax and tax input with automatic calculation. Saves only price with tax.

Price::make()
    ->withAllFieldOnForm()

With custom column names:

Price::make()
    ->withAllFieldOnForm(withoutTaxColumn: 'price_without_tax', taxColumn: 'tax')

Using price inputs without tax input

Price::make()
    ->withAllFieldOnForm()
    ->hideTaxFormField()
    ->setTax(23)

Using predefined tax options

Price::make()
    ->withAllFieldOnForm()
    ->taxFieldAsSelect([
        10 => ['name' => 'Reduced tax (10%)'],
        23 => ['name' => 'Standard tax (23%)'],
    ])

Save the selected option ID:

Price::make()
    ->withAllFieldOnForm()
    ->taxFieldAsSelect([
        10 => ['id' => 'reduced',  'name' => 'Reduced tax (10%)'],
        23 => ['id' => 'standard', 'name' => 'Standard tax (23%)'],
    ], 'vat_rate_type')

Save the option value (key) instead of ID:

Price::make()
    ->withAllFieldOnForm()
    ->taxFieldAsSelect([
        10 => ['id' => 'reduced',  'name' => 'Reduced tax (10%)'],
        23 => ['id' => 'standard', 'name' => 'Standard tax (23%)'],
    ], 'vat_rate_type', true)

wamesk/laravel-nova-price-field 适用场景与选型建议

wamesk/laravel-nova-price-field 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 234 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nova」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 wamesk/laravel-nova-price-field 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-14