PHP code example of gldrenthe89 / nova-calculated-field

1. Go to this page and download the library: Download gldrenthe89/nova-calculated-field library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gldrenthe89 / nova-calculated-field example snippets




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;
                })
        ];
    }
}