gabrielesbaiz / nova-ajax-select
Ajax select / child select package for Laravel Nova.
Fund package maintenance!
Gabriele Sbaiz
Requires
- php: ^8.0
- illuminate/contracts: ^10.0||^11.0||^12.0
- laravel/nova: ^4.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
README
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.