Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin authored Aug 3, 2020
1 parent 17c6fd8 commit bb5630f
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
# Dynamic select field for Laravel Nova
# Dynamic select field for Laravel Nova (maintained repo)

This field allows you to dynamically fill contents of a select based on values in other dynamic select fields.

Field is based on [nova-belongs-to-dependency]{https://novapackages.com/packages/manmohanjit/nova-belongs-to-dependency}
but instead of selecting model, you can select custom values.
Field is based on [nova-belongs-to-dependency](https://github.com/manmohanjit/nova-belongs-to-dependency) but instead of selecting model, you can select custom values.

## Installation

```
composer require royduin/laravel-nova-field-dynamic-select
```

## Usage

### Usage
Class have 2 special methods on top of default Select from Laravel Nova.
`dependsOn` can take a list of other fields this one depends on.
`options` can be either an array or a callable.
If its a callable, it will receive array with selected dependency values as
first argument and should return an array of items to be shown on the select field.
- `dependsOn` can take a list of other fields this one depends on.
- `options` can be either an array or a callable.

If its a callable, it will receive array with selected dependency values as first argument and should return an array of items to be shown on the select field.


### Example:
## Example

```
public function fields(Request $request)
{
return [
ID::make()->sortable(),
DynamicSelect::make('Country', 'country')
->options(['US' => 'United States', 'UK' => 'United Kingdom'])
->rules('required')
,
DynamicSelect::make('Provider', 'provider')
->options(['PR' => 'Premium', 'ST' => 'Standard'])
->rules('required')
,
DynamicSelect::make('Product', 'product')
->dependsOn(['country', 'provider'])
->options(function($values) {
if($values['country'] === 'UK' && $values['provider'] === 'PR') {
return ['A' => 'Fast shipping', 'B' => 'Normal shipping', 'C' => 'Free shipping'];
} else {
return ['A' => 'Fast shipping', 'B' => 'Normal shipping'];
}
})
->rules('required')
,
];
}
{
return [
ID::make()->sortable(),
DynamicSelect::make('Country', 'country')
->options(['US' => 'United States', 'UK' => 'United Kingdom'])
->rules('required')
,
DynamicSelect::make('Provider', 'provider')
->options(['PR' => 'Premium', 'ST' => 'Standard'])
->rules('required')
,
DynamicSelect::make('Product', 'product')
->dependsOn(['country', 'provider'])
->options(function($values) {
if($values['country'] === 'UK' && $values['provider'] === 'PR') {
return ['A' => 'Fast shipping', 'B' => 'Normal shipping', 'C' => 'Free shipping'];
} else {
return ['A' => 'Fast shipping', 'B' => 'Normal shipping'];
}
})
->rules('required')
,
];
}
```

0 comments on commit bb5630f

Please sign in to comment.