sanderbaas/laravel-nova-belongsto-dependency

This package is abandoned and no longer maintained. No replacement package was suggested.

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

v1.0.2 2019-11-26 15:52 UTC

This package is auto-updated.

Last update: 2020-12-26 18:28:31 UTC


README

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.