https://github.com/dillingham/nova-ajax-select
Ajax select / child select package for Laravel Nova
https://github.com/dillingham/nova-ajax-select
laravel laravel-nova
Last synced: 3 months ago
JSON representation
Ajax select / child select package for Laravel Nova
- Host: GitHub
- URL: https://github.com/dillingham/nova-ajax-select
- Owner: dillingham
- License: mit
- Created: 2019-02-10T20:52:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-29T12:08:13.000Z (about 1 year ago)
- Last Synced: 2025-03-29T13:07:34.314Z (4 months ago)
- Topics: laravel, laravel-nova
- Language: Vue
- Homepage:
- Size: 167 KB
- Stars: 87
- Watchers: 2
- Forks: 22
- Open Issues: 14
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# Nova Ajax Select
[](https://packagist.org/packages/dillingham/nova-ajax-select)
[](https://packagist.org/packages/dillingham/nova-ajax-select) [](https://twitter.com/im_brian_d)Ajax populated select fields based on the values of other fields and when they change.

### Install
```
composer require dillingham/nova-ajax-select
```### Usage
Specify a request url & optionally the `parent($attribute)` to watch & trigger the ajax select:
```php
use NovaAjaxSelect\AjaxSelect;
```
```php
BelongsTo::make('Company'),AjaxSelect::make('User')
->get('/api/company/{company}/users')
->parent('company'),
```
Add the field for index & detail views display. AjaxSelect is for forms only
```php
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:
```php
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`:
```php
Select::make('Country')
->options([]),AjaxSelect::make('State')
->get('/api/country/{country}/states')
->parent('country'),AjaxSelect::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`
```php
BelongsTo::make('Project'),AjaxSelect::make('File')
->get('/{project}/files')
->parent('project'),AjaxSelect::make('Comment')
->get('/{project}/comments')
->parent('project'),
```---
# Author
Hi 👋, Im Brian Dillingham, creator of this Nova package [and others](https://novapackages.com/collaborators/dillingham)
Hope you find it useful. Feel free to reach out with feedback.
Follow me on twitter: [@im_brian_d](https://twitter.com/im_brian_d)