Benjacho / belongs-to-many-field-nova

Belongs To Many field Laravel nova to represent many to many relationship in field.
MIT License
157 stars 80 forks source link

Filter by field? #81

Closed kirgy closed 3 years ago

kirgy commented 3 years ago

What I'm trying to achieve is to specify a list of options only when certain criteria on the database record are met. For example, for a table of users has a many-to-many relationship to countries;

CREATE TABLE `countries` (
  `id` bigint(20) unsigned,
  `name` varchar,
  `iso_code` varchar,
  `active` tinyint(1),
)

CREATE TABLE `countries_users` (
  `id` bigint(20) unsigned,
  `country_id` bigint(20) unsigned,
  `user_id` int(10) unsigned,
)

I want to filter to only show countries which have an active value of 1 (true).

Is something like this achievable?

            BelongsToManyField::make('Operating Countries', 'countries', \App\Nova\Country::class)
                ->filter(function($query) {
                   return $query->where('active', '=', true);
                })

If this is an unsupported feature, it would be useful for me to add this to this repo - if it would be of value.

Benjacho commented 3 years ago

Actually you have options method that supports a callback function, test it.