F
Filament2y ago
Abi

Dependent MorphToSelect Field

I have 2 MorphToSelect fields, is it possible to make the 2nd one dependable on the first one, like with the Select field? I want to filter the values on the 2nd MorphToSelect field based on the value select on the first MorphToSelect field. I only want to filter the values and not the Type. I see there is the modifyOptionsQueryUsing function on MorphToSelect\Type, but not sure how to change the query based on another MorphToSelect fields data.
10 Replies
Abi
AbiOP2y ago
@awcodes appreciate if you have any insight on this. Something to learn for me today 🙂
awcodes
awcodes2y ago
Can you provide any code that you've tried?
Abi
AbiOP2y ago
MorphToSelect::make('vendor')
->types(function () {
return [
MorphToSelect\Type::make(USPS::class)->label('USPS')->titleColumnName('name'),
MorphToSelect\Type::make(UPS::class)->label('UPS')->titleColumnName('name'),
];
})->reactive(),

MorphToSelect::make('packages')
->types(function () {
return [
MorphToSelect\Type::make(PremiumPackageType::class)->label('Package Types')
->titleColumnName('name')
->modifyOptionsQueryUsing(fn(Builder $query, callable $get) => $query->where('vendor_id', '=', $get('vendor'))),
MorphToSelect\Type::make(NormalPackageType::class)->label('Package Types')
->titleColumnName('name')
->modifyOptionsQueryUsing(fn(Builder $query, callable $get) => $query->where('vendor_id', '=', $get('vendor'))),
MorphToSelect\Type::make(InternationalPackageType::class)->label('Package Types')
->titleColumnName('name')
->modifyOptionsQueryUsing(fn(Builder $query, callable $get) => $query->where('vendor_id', '=', $get('vendor')))
];
}),
MorphToSelect::make('vendor')
->types(function () {
return [
MorphToSelect\Type::make(USPS::class)->label('USPS')->titleColumnName('name'),
MorphToSelect\Type::make(UPS::class)->label('UPS')->titleColumnName('name'),
];
})->reactive(),

MorphToSelect::make('packages')
->types(function () {
return [
MorphToSelect\Type::make(PremiumPackageType::class)->label('Package Types')
->titleColumnName('name')
->modifyOptionsQueryUsing(fn(Builder $query, callable $get) => $query->where('vendor_id', '=', $get('vendor'))),
MorphToSelect\Type::make(NormalPackageType::class)->label('Package Types')
->titleColumnName('name')
->modifyOptionsQueryUsing(fn(Builder $query, callable $get) => $query->where('vendor_id', '=', $get('vendor'))),
MorphToSelect\Type::make(InternationalPackageType::class)->label('Package Types')
->titleColumnName('name')
->modifyOptionsQueryUsing(fn(Builder $query, callable $get) => $query->where('vendor_id', '=', $get('vendor')))
];
}),
Basically I am trying to load the package Type based on the vendor being selected. And show only the packages that belong to that vendor
Abi
AbiOP2y ago
I did see the documentation here https://filamentphp.com/docs/2.x/forms/fields#handling-morphto-relationships, but the use of $this inside the closure of the method modifyOptionsQueryUsing throws an error
Filament
Fields - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
Abi
AbiOP2y ago
This is the error I get when using $this on the closure
Using $this when not in object context
Using $this when not in object context
awcodes
awcodes2y ago
This is a lot to digest and really complicated. 🤣
Dan Harrin
Dan Harrin2y ago
where are you using $this what for
Abi
AbiOP2y ago
I used $this as shown on the docs and got an error, but then switched the code to use the Closure like I would use with the Select field. All I am trying to do is modify the query for the MorphToSelect to filter the values it shows based on the previous MorphToSelect
Dan Harrin
Dan Harrin2y ago
the example isnt nessecary for admin resources specifically, which is why we use $this so try $get('vendor_id')
Abi
AbiOP2y ago
ok, let me try that. show I use the _id column of the morph there. Perfect. That worked. I appreciate the help
Want results from more Discord servers?
Add your server