This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
forked from hubertnnn/laravel-nova-field-dynamic-select
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
, | ||
]; | ||
} | ||
``` | ||
|