saumini / count
A Laravel Nova field.
Installs: 426 131
Dependents: 1
Suggesters: 0
Security: 0
Stars: 29
Watchers: 2
Forks: 7
Open Issues: 2
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2024-10-28 05:46:05 UTC
README
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'); } }