Skip to content
A Server Side Calculated Field for Laravel Nova
Vue PHP JavaScript CSS
Branch: master
Clone or download
codebykyle Merge pull request #3 from RingierIMU/master
Send through the ResourceId from Listener Field
Latest commit 9216d02 Oct 10, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist Add numberFormat property Oct 10, 2019
resources Add numberFormat property Oct 10, 2019
routes Initial commit Aug 12, 2019
src Add numberFormat property Oct 10, 2019
.gitignore
README.md Update README.md Aug 15, 2019
composer.json corrected tags Aug 12, 2019
package.json Add numberFormat property Oct 10, 2019
webpack.mix.js Initial commit Aug 12, 2019
yarn.lock Add numberFormat property Oct 10, 2019

README.md

A Server-Side Calculated Field For Laravel Nova

This package contains two Nova fields required to do server-side calculations from the Nova client. The user can optionally override the calculated value on the form.

For a detailed write-up of the how-and-why of this field, please visit:

https://codebykyle.com/blog/laravel-nova-custom-calculated-field

Installation

Install the package via composer:

composer require codebykyle/calculated-field

Example

For example:

As a number

Calculated Number Field

As a string:

Calculated String Field

Default

The Listener field will by default sum all numbers passed to it

Usage

<?php

use Codebykyle\CalculatedField\BroadcasterField;
use Codebykyle\CalculatedField\ListenerField;

class MyResource extends Resource
{
    public function fields(Request $request) {
        return [    
            BroadcasterField::make('Sub Total', 'sub_total'),
            BroadcasterField::make('Tax', 'tax'),
            ListenerField::make('Total Field', 'total_field')
        ];
    }
}

Overriding the Callback


<?php
use Codebykyle\CalculatedField\BroadcasterField;
use Codebykyle\CalculatedField\ListenerField;

class MyResource extends Resource
{
    public function fields(Request $request) {
        return [    
            BroadcasterField::make('Sub Total', 'sub_total'),
            BroadcasterField::make('Tax', 'tax'),

            ListenerField::make('Total Field', 'total_field')
                ->calculateWith(function (Collection $values) {
                    $subtotal = $values->get('sub_total');
                    $tax = $values->get('tax');
                    return $subtotal + $tax;
                }),
        ];
    }
}

String Fields


<?php
use Codebykyle\CalculatedField\BroadcasterField;
use Codebykyle\CalculatedField\ListenerField;

class MyResource extends Resource
{
    public function fields(Request $request) {
        return [    
            BroadcasterField::make('First Name', 'first_name')
                ->setType('string'),

            BroadcasterField::make('Last Name', 'last_name')
                ->setType('string'),

            ListenerField::make('Full Name', 'full_name')
                ->calculateWith(function (Collection $values) {
                    return $values->values()->join(' ');
                }),
        ];
    }
}

Multiple Calculated Fields


<?php
use Codebykyle\CalculatedField\BroadcasterField;
use Codebykyle\CalculatedField\ListenerField;

class MyResource extends Resource
{
    public function fields(Request $request) {
        return [    
            BroadcasterField::make('Sub Total', 'sub_total')
                ->broadcastTo('total'),

            BroadcasterField::make('Tax', 'tax')
                ->broadcastTo('total'),

            ListenerField::make('Total Field', 'total_field')
                ->listensTo('total')
                ->calculateWith(function (Collection $values) {
                    $subtotal = $values->get('sub_total');
                    $tax = $values->get('tax');
                    return $subtotal + $tax;
                }),


            BroadcasterField::make('Senior Discount', 'senior_discount')
                ->broadcastTo('discount'),
    
            BroadcasterField::make('Coupon Discount', 'coupon_amount')
                ->broadcastTo('discount'),
    
            ListenerField::make('Total Discount', 'total_discount')
                ->listensTo('discount')
                ->calculateWith(function (Collection $values) {
                    $seniorDiscount = $values->get('senior_discount');
                    $couponAmount = $values->get('coupon_amount');
    
                    return $seniorDiscount + $couponAmount;
                })
        ];
    }
}
You can’t perform that action at this time.