nova-field-count icon indicating copy to clipboard operation
nova-field-count copied to clipboard

Simple trick

Open StanBarrows opened this issue 6 years ago • 9 comments

Heyho, just a question: Why so "complicated"?

  Number::make('Roles Count', function () {
                return $this->roles->count();
            })

StanBarrows avatar Nov 14 '18 19:11 StanBarrows

@StanBarrows Hey yo, I'm aware of it. I created this for fun, doesn't harm :)

nsaumini avatar Nov 15 '18 03:11 nsaumini

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: image

d4rkd0s avatar Aug 11 '19 19:08 d4rkd0s

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?

nikuscs avatar Sep 11 '19 18:09 nikuscs

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 avatar Dec 17 '19 11:12 bernhardh

@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();
                        }]);
                    }]);
}

monaye avatar Jun 18 '20 23:06 monaye

Sorry, I don't know.

bernhardh avatar Jun 19 '20 08:06 bernhardh

@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.

monaye avatar Jun 25 '20 15:06 monaye

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 avatar Feb 08 '22 03:02 bilogic

@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(),
    ];
}

leith avatar Nov 08 '22 20:11 leith