sanderbaas/laravel-nova-belongsto-dependency

Extend Laravel Nova so that searchable BelongsTo fields can be filtered by values of other fields

Downloads

2730

Stars

1

Version

v1.0.2

This package has been archived

BelongsTo field with dependency support for Nova

This package adds extra parameters to the request used by the BelongsTo fields to fetch the options. The extra parameters are the selected values of other fields so they can be used to filter the options based on selections in other fields. The extra parameters are only passed when using a BelongsTo field that is searchable.

Installation

You can install this package on a Laravel app that uses Nova via composer:

composer require sanderbaas/laravel-nova-belongsto-dependency

Usage

Make sure the fields of which the values should be filtered based on selections of other fields are ->searchable().

return [
    ...
    BelongsTo::make('Company'),
    BelongsTo::make('Employee')
    ->searchable(),
    ...
];

Then on the Employee resource, in this case, use the extra fields to filter the correct employees as follows:

public static function relatableQuery(NovaRequest $request, $query)
{
    $query->where('company_id', '=', $request->selectedCompany);
    return $query;
}

Credits

I got the idea of adding selected values to the request from experience with Backpack for Laravel. Also I was inspired by the package of manmohanjit: nova-belongsto-dependency

License

The MIT License (MIT). Please see License File for more information.

sanderbaas

Author

sanderbaas