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.
[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.
Install the package via composer:
composer require gldrenthe89/nove-calculated-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;
})
];
}
}