DEV Community

Cover image for Laravel Nova - Dependent Filter
Jenry Mazariegos
Jenry Mazariegos

Posted on

Laravel Nova - Dependent Filter

Hi guys! Today I want to share a new custom component I built for Laravel Nova 4/5.

The idea behind this package is simple: cascading dependent filters. If you've ever needed your Nova filters to talk to each other — where selecting a value in one filter automatically narrows the options in the next — this is for you.

For example, imagine you have Clients, Projects, and Users. When you select a client, the Project filter should only show projects belonging to that client. Then, when you pick a project, the User filter narrows down to only the users assigned to that project.

Nova doesn't support this out of the box, and building it from scratch every time is tedious. So I packaged it up into a clean, reusable solution:

use DevJM\DependentFilter\Nova\Filters\DependentFilter;

$client = DependentFilter::make('Client', Client::class, 'client_id');

$project = DependentFilter::make('Project', Project::class, 'project_id')
    ->dependsOn($client, foreignKey: 'client_id');

$user = DependentFilter::make('User', User::class, 'user_id')
    ->dependsOn($project, relationship: 'projects');

return [$client, $project, $user];
Enter fullscreen mode Exit fullscreen mode

Three lines. Three cascading filters. No boilerplate.

Check it out

Top comments (0)