Laravel Community Tools by Tighten
Laravel Nova icon Nova Packages
N

Calculated Field

Readme

This packages is created after the package from codebykyle But after an extensive refactor and updating a lot of VueJS code to the latest Laravel Nova code I made this in to a new package.

New features

[Release V2.3]
-- Fixed issue where you can't edit a listener field
-- Added Date Broadcaster field
-- Added Date Listener field
-- Added 'calculate' buttons to all visible Listener Fields
-- Added ability to turn of calculation on update forms.

[changes up to V2.2]
-- BelongsTo Broadcaster field
-- MorphTo Broadcaster field
-- Currency Listener Field
-- Hidden Listener Field
-- Code has been completely updated to latest Nova (2021-03-18)

Below pieces of the old Documentation from codebykyle I'm not really good at writing documentation. So please feel free to creat a PR for it.

Installation

Install the package via composer:

composer require gldrenthe89/nova-calculated-field

Example

Calculated Number Field

<?php
use Gldrenthe89\NovaCalculatedField\BroadcasterField;
use Gldrenthe89\NovaCalculatedField\ListenerField;

class MyResource extends Resource
{
    public function fields(Request $request) {
        return [    
            BroadcasterField::make('Sub Total', 'sub_total')
                ->broadcastTo('total'), // can either be a String or an Array

            BroadcasterField::make('Tax', 'tax')
                ->broadcastTo('total'), // can either be a String or an Array

            ListenerField::make('Total Field', 'total_field')
                ->listensTo('total') // can either be a String or an Array
                ->calculateWith(function (Collection $values) {
                    $subtotal = $values->get('sub_total');
                    $tax = $values->get('tax');
                    return $subtotal + $tax;
                }),


            BroadcasterField::make('Senior Discount', 'senior_discount')
                ->broadcastTo('discount'), // can either be a String or an Array
    
            BroadcasterField::make('Coupon Discount', 'coupon_amount')
                ->broadcastTo('discount'), // can either be a String or an Array
    
            ListenerField::make('Total Discount', 'total_discount')
                ->listensTo('discount') // can either be a String or an Array
                ->disableCalculationOnUpdate() // Only when to disable on Update forms
                ->calculateWith(function (Collection $values) {
                    $seniorDiscount = $values->get('senior_discount');
                    $couponAmount = $values->get('coupon_amount');
    
                    return $seniorDiscount + $couponAmount;
                })
        ];
    }
}

Reviews

By Emile Jobity on September 14, 2022

Can this be made compatible with Nova 4?

Back to Top
Added 5 years ago
Last updated 3 Years Ago
Version v2.4.1
Nova Version ~3.0
Composer
gldrenthe89/nova-calculated-field
GitHub stars 5
Packagist downloads 14,201

Favorites

2 users favorited

Rating

3.50
(out of 5)
★★★★★
★★★★
★★★
★★
2 ratings

Brought to you by Tighten

Issues/Feature Requests Stats Package Ideas