codebykyle / calculated-field
A server side calculated field for Laravel Nova
Installs: 69 044
Dependents: 0
Suggesters: 0
Security: 0
Stars: 45
Watchers: 3
Forks: 22
Open Issues: 28
Language:Vue
Requires
- php: >=7.1.0
- dev-master
- 1.1
- 1.0
- dev-dependabot/npm_and_yarn/express-4.18.2
- dev-dependabot/npm_and_yarn/qs-6.5.3
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/loader-utils-1.4.2
- dev-dependabot/npm_and_yarn/url-parse-1.5.10
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-dependabot/npm_and_yarn/dns-packet-1.3.4
- dev-dependabot/npm_and_yarn/hosted-git-info-2.8.9
- dev-dependabot/npm_and_yarn/lodash-4.17.21
- dev-dependabot/npm_and_yarn/y18n-3.2.2
- dev-dependabot/npm_and_yarn/elliptic-6.5.4
- dev-dependabot/npm_and_yarn/ini-1.3.8
- dev-dependabot/npm_and_yarn/http-proxy-1.18.1
- dev-dependabot/npm_and_yarn/node-sass-4.14.1
- dev-dependabot/npm_and_yarn/websocket-extensions-0.1.4
This package is not auto-updated.
Last update: 2024-03-16 01:54:46 UTC
README
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
As a string:
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; }) ]; } }