gabrielesbaiz/nova-ajax-select

Ajax select / child select package for Laravel Nova.

1.0.0 2025-03-03 06:02 UTC

This package is auto-updated.

Last update: 2025-03-03 06:03:49 UTC


README

Latest Version on Packagist Total Downloads

Ajax select / child select package for Laravel Nova.

Original code from alexwenzel/nova-ajax-select

Features

  • ✅ Ajax populated select fields based on the values of other fields and when they change

Installation

You can install the package via composer:

composer require gabrielesbaiz/nova-ajax-select

Usage

$novaAjaxSelect = new Gabrielesbaiz\NovaAjaxSelect();
echo $novaAjaxSelect->echoPhrase('Hello, Gabrielesbaiz!');

Specify a request url & optionally the parent($attribute) to watch & trigger the ajax select:

use Gabrielesbaiz\NovaAjaxSelect\NovaAjaxSelect;;
BelongsTo::make('Company'),

NovaAjaxSelect::make('User')
    ->get('/api/company/{company}/users')
    ->parent('company'),

Add the field for index & detail views display. NovaAjaxSelect is for forms only

BelongsTo::make('User')->exceptOnForms(),

Request Url:

In the above example, we say company is the parent.

The {company} url parameter will equal the selected Company field value.

Response Format:

The select field expects a value & display. Map your results like so:

Route::get('api/company/{company}/users', function($company_id) {

    $company = \App\Company::find($company_id);

    return $company->users->map(function($user) {
        return [ 'value' => $user->id, 'display' => $user->name ];
    });
})->middleware(['nova']);

Make children depend on other children

City makes a request based on State, which makes a request based on Country:

Select::make('Country')
    ->options([]),

NovaAjaxSelect::make('State')
    ->get('/api/country/{country}/states')
    ->parent('country'),

NovaAjaxSelect::make('City')
    ->get('/api/state/{state}/cities')
    ->parent('state'),

Make multiple children depend on one parent

File & Comment will both make a request based on Project

BelongsTo::make('Project'),

NovaAjaxSelect::make('File')
    ->get('/{project}/files')
    ->parent('project'),

NovaAjaxSelect::make('Comment')
    ->get('/{project}/comments')
    ->parent('project'),

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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