A Laravel Nova field for relationship count. Displays only on index view.
> composer require saumini/count
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'),
];
}
}
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');
}
}