├── .docs └── Screenshot1.png ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── dist ├── css │ └── field.css ├── js │ └── field.js └── mix-manifest.json ├── package.json ├── resources ├── js │ ├── components │ │ └── IndexField.vue │ └── field.js └── sass │ └── field.scss ├── src ├── FieldServiceProvider.php └── RelationshipCount.php └── webpack.mix.js /.docs/Screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nsaumini/nova-field-count/d2ca2cdd106a4efa1b118fbfe73a5f9ed3443164/.docs/Screenshot1.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | /node_modules 4 | package-lock.json 5 | composer.phar 6 | composer.lock 7 | phpunit.xml 8 | .phpunit.result.cache 9 | .DS_Store 10 | Thumbs.db 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [v1.0.1] - 2018-11-16 4 | ### Changed 5 | - Renamed class name Count to RelationshipCount 6 | 7 | ## [v1.0.0] - 2018-11-14 8 | ### Added 9 | - Initial release 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Saumini Navaratnam 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nova Field Count 2 | A Laravel Nova field for relationship count. Displays only on index view. 3 | 4 | ## Installation 5 | 6 | ```bash 7 | > composer require saumini/count 8 | ``` 9 | 10 | ## Usage 11 | 12 | Define the relationship on model. 13 | 14 | ```php 15 | class Post extends Model 16 | { 17 | public function comments() 18 | { 19 | return $this->hasMany('App\Comment'); 20 | } 21 | } 22 | 23 | ``` 24 | 25 | Use `Count` field on relationship. 26 | 27 | ```php 28 | use Saumini\Count\RelationshipCount; 29 | 30 | class Survey extends Resource 31 | { 32 | ... 33 | public function fields(Request $request) 34 | { 35 | return [ 36 | RelationshipCount::make('Comments Count', 'comments'), 37 | ]; 38 | } 39 | } 40 | ``` 41 | 42 | #### For sortable field 43 | 44 | ```php 45 | use Saumini\Count\RelationshipCount; 46 | 47 | class Survey extends Resource 48 | { 49 | ... 50 | public function fields(Request $request) 51 | { 52 | return [ 53 | RelationshipCount::make('Comments Count', 'comments')->sortable(), 54 | ]; 55 | } 56 | 57 | // Overwrite the indexQuery to include relationship count 58 | public static function indexQuery(NovaRequest $request, $query) 59 | { 60 | // Give relationship name as alias else Laravel will name it as comments_count 61 | return $query->withCount('comments as comments'); 62 | } 63 | } 64 | ``` 65 | 66 | ## Screenshot 67 | 68 | ![Screenshot 1](https://raw.githubusercontent.com/nsaumini/nova-field-count/master/.docs/Screenshot1.png) 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "saumini/count", 3 | "description": "A Laravel Nova field.", 4 | "keywords": [ 5 | "laravel", 6 | "nova" 7 | ], 8 | "license": "MIT", 9 | "require": { 10 | "php": ">=7.1.0" 11 | }, 12 | "autoload": { 13 | "psr-4": { 14 | "Saumini\\Count\\": "src/" 15 | } 16 | }, 17 | "extra": { 18 | "laravel": { 19 | "providers": [ 20 | "Saumini\\Count\\FieldServiceProvider" 21 | ] 22 | } 23 | }, 24 | "config": { 25 | "sort-packages": true 26 | }, 27 | "minimum-stability": "dev", 28 | "prefer-stable": true 29 | } 30 | -------------------------------------------------------------------------------- /dist/css/field.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nsaumini/nova-field-count/d2ca2cdd106a4efa1b118fbfe73a5f9ed3443164/dist/css/field.css -------------------------------------------------------------------------------- /dist/js/field.js: -------------------------------------------------------------------------------- 1 | !function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){n(1),e.exports=n(6)},function(e,t,n){Nova.booting(function(e,t){e.component("index-count",n(2))})},function(e,t,n){var r=n(3)(n(4),n(5),!1,null,null,null);e.exports=r.exports},function(e,t){e.exports=function(e,t,n,r,o,i){var s,u=e=e||{},c=typeof e.default;"object"!==c&&"function"!==c||(s=e,u=e.default);var f,a="function"==typeof u?u.options:u;if(t&&(a.render=t.render,a.staticRenderFns=t.staticRenderFns,a._compiled=!0),n&&(a.functional=!0),o&&(a._scopeId=o),i?(f=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),r&&r.call(this,e),e&&e._registeredComponents&&e._registeredComponents.add(i)},a._ssrRegister=f):r&&(f=r),f){var l=a.functional,d=l?a.render:a.beforeCreate;l?(a._injectStyles=f,a.render=function(e,t){return f.call(t),d(e,t)}):a.beforeCreate=d?[].concat(d,f):[f]}return{esModule:s,exports:u,options:a}}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default={props:["resourceName","field"]}},function(e,t){e.exports={render:function(){var e=this.$createElement;return(this._self._c||e)("span",[this._v(this._s(this.field.value))])},staticRenderFns:[]}},function(e,t){}]); -------------------------------------------------------------------------------- /dist/mix-manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "/js/field.js": "/js/field.js", 3 | "/css/field.css": "/css/field.css" 4 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "dev": "npm run development", 5 | "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", 6 | "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", 7 | "watch-poll": "npm run watch -- --watch-poll", 8 | "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js", 9 | "prod": "npm run production", 10 | "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" 11 | }, 12 | "devDependencies": { 13 | "cross-env": "^5.0.0", 14 | "laravel-mix": "^1.0", 15 | "laravel-nova": "^1.0" 16 | }, 17 | "dependencies": { 18 | "vue": "^2.5.0" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /resources/js/components/IndexField.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 10 | -------------------------------------------------------------------------------- /resources/js/field.js: -------------------------------------------------------------------------------- 1 | Nova.booting((Vue, router) => { 2 | Vue.component('index-count', require('./components/IndexField')); 3 | }) 4 | -------------------------------------------------------------------------------- /resources/sass/field.scss: -------------------------------------------------------------------------------- 1 | // Nova Tool CSS 2 | -------------------------------------------------------------------------------- /src/FieldServiceProvider.php: -------------------------------------------------------------------------------- 1 | attribute; 54 | 55 | $this->value = $resource->$attribute()->count(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /webpack.mix.js: -------------------------------------------------------------------------------- 1 | let mix = require('laravel-mix') 2 | 3 | mix.setPublicPath('dist') 4 | .js('resources/js/field.js', 'js') 5 | .sass('resources/sass/field.scss', 'css') 6 | --------------------------------------------------------------------------------