saumini/count

A Laravel Nova field.
572,685 28
Install
composer require saumini/count
Latest Version:v1.0.1
PHP:>=7.1.0
License:MIT
Last Updated:Apr 27, 2019
Links: GitHub  ·  Packagist
Maintainer: nsaumini

Nova Field Count

A Laravel Nova field for relationship count. Displays only on index view.

Installation

> composer require saumini/count

Usage

Define the relationship on model.

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

Use Count field on relationship.

use Saumini\Count\RelationshipCount;

class Survey extends Resource
{
    ...
    public function fields(Request $request)
    {
        return [
            RelationshipCount::make('Comments Count', 'comments'),
        ];
    }
}

For sortable field

use Saumini\Count\RelationshipCount;

class Survey extends Resource
{
    ...
    public function fields(Request $request)
    {
        return [
            RelationshipCount::make('Comments Count', 'comments')->sortable(),
        ];
    }
    
    // Overwrite the indexQuery to include relationship count
    public static function indexQuery(NovaRequest $request, $query)
    {
        // Give relationship name as alias else Laravel will name it as comments_count
        return $query->withCount('comments as comments');
    }
}

Screenshot

Screenshot 1

Related Packages

maatwebsite/laravel-nova-excel

Supercharged Excel exports for Laravel Nova Resources

7,034,737 401
vyuldashev/nova-permission

A Laravel Nova tool for Spatie's Permission library.

2,632,252 430
unm/laravel4-opencloud

A laravel 4.1 service provider package that wraps the Rackspace/Openstack PHP SD...

4 7
spatie/nova-backup-tool

A Laravel Nova tool to backup your application.

599,910 360
spatie/nova-tail-tool

A Laravel Nova tool to display the application log.

129,750 119