nova-field-count
nova-field-count copied to clipboard
Simple trick
Heyho, just a question: Why so "complicated"?
Number::make('Roles Count', function () {
return $this->roles->count();
})
@StanBarrows Hey yo, I'm aware of it. I created this for fun, doesn't harm :)
Heyho, just a question: Why so "complicated"?
Number::make('Roles Count', function () { return $this->roles->count(); })
I think there should be a "count" example in the docs somewhere maybe where Number is: https://nova.laravel.com/docs/2.0/resources/fields.html#number-field
When you search count you only get these:

Heyho, just a question: Why so "complicated"?
Number::make('Roles Count', function () { return $this->roles->count(); })
But with your approach, you won't be able to sort? If I'm aware of?
It will work exactly like it works in this package. You will need to add the count to the query in BOTH cases:
public function fields()
{
return [
Number::make('Roles count', 'rolesCount', function () {
return $this->roles->count();
})->sortable(true);
];
}
public static function indexQuery(NovaRequest $request, $query)
{
// Give relationship name as alias else Laravel will name it as roles_count
return $query->withCount('roles as rolesCount');
}
The biggest advantage of using regular Number instead of this package is, that this package conflicts with hasMany Relationship field, since you will have 2 fields with the attribute name roles which will not work:
public function fields(Request $request)
{
return [
RelationshipCount::make('Roles Count', 'roles'),
HasMany::make("Roles", "roles", Role::class)
];
}
@bernhardh Is there a way to add the nested relation count field?
Here is the example: try to get the valid permission count on user => role => permission relation. valid is scoped in the permission model.
// this give me the unknown column permissionCounts
public function fields()
{
return [
Number::make('Count of Valid Permission', 'validPermissionCount', function () {
return $this->roles->permission()->valid()->count();
})->sortable(true);
];
}
public static function indexQuery(NovaRequest $request, $query)
return $query->with(['role' => function ($query) {
$query->withCount(['permission as validPermissionCount' => function ($query) {
$query->valid();
}]);
}]);
}
Sorry, I don't know.
@bernhardh Thank you for your reply :)
I was able to solve my issue by utilizing the hasManyThrough relation so I don't have to use nested stuff.
It will work exactly like it works in this package. You will need to add the count to the query in BOTH cases:
public function fields() { return [ Number::make('Roles count', 'rolesCount', function () { return $this->roles->count(); })->sortable(true); ]; } public static function indexQuery(NovaRequest $request, $query) { // Give relationship name as alias else Laravel will name it as roles_count return $query->withCount('roles as rolesCount'); }
I wished there was a way to get this done (count + sort) without the indexQuery()
@bilogic If you don't want to mess with the indexQuery(), you can add it to your model as a scope:
use Illuminate\Database\Eloquent\Builder;
protected static function booted()
{
static::addGlobalScope('with-role-count', function (Builder $builder) {
$builder->withCount('roles');
});
}
Now on your Nova component:
public function fields()
{
return [
Number::make('Roles', 'roles_count')
// this works now
->sortable(),
];
}