{"id":21504434,"url":"https://github.com/dcasia/conditional-container","last_synced_at":"2025-04-05T02:08:44.774Z","repository":{"id":35147585,"uuid":"212774331","full_name":"dcasia/conditional-container","owner":"dcasia","description":"Provides an easy way to conditionally show and hide fields in your Nova resources.","archived":false,"fork":false,"pushed_at":"2022-11-14T15:23:22.000Z","size":513,"stargazers_count":115,"open_issues_count":24,"forks_count":37,"subscribers_count":6,"default_branch":"development","last_synced_at":"2024-09-19T11:44:21.030Z","etag":null,"topics":["conditional","conditional-field","dependency","laravel","laravel-nova-field","nova"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcasia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-04T08:56:30.000Z","updated_at":"2024-06-06T11:07:25.000Z","dependencies_parsed_at":"2023-01-15T14:47:16.879Z","dependency_job_id":null,"html_url":"https://github.com/dcasia/conditional-container","commit_stats":null,"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcasia%2Fconditional-container","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcasia%2Fconditional-container/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcasia%2Fconditional-container/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcasia%2Fconditional-container/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcasia","download_url":"https://codeload.github.com/dcasia/conditional-container/tar.gz/refs/heads/development","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276164,"owners_count":20912288,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conditional","conditional-field","dependency","laravel","laravel-nova-field","nova"],"created_at":"2024-11-23T19:00:03.709Z","updated_at":"2025-04-05T02:08:44.745Z","avatar_url":"https://github.com/dcasia.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Conditional Container\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/digital-creative/conditional-container)](https://packagist.org/packages/digital-creative/conditional-container)\n[![Total Downloads](https://img.shields.io/packagist/dt/digital-creative/conditional-container)](https://packagist.org/packages/digital-creative/conditional-container)\n[![License](https://img.shields.io/packagist/l/digital-creative/conditional-container)](https://github.com/dcasia/conditional-container/blob/master/LICENSE)\n\n![Laravel Nova Conditional Container in action](https://raw.githubusercontent.com/dcasia/conditional-container/master/demo.gif)\n\nProvides an easy way to conditionally show and hide fields in your Nova resources.\n\n# Installation\n\nYou can install the package via composer:\n\n```\ncomposer require digital-creative/conditional-container\n```\n\n## Usage\n\nBasic demo showing the power of this field:\n\n```php\n\nuse DigitalCreative\\ConditionalContainer\\ConditionalContainer;\nuse DigitalCreative\\ConditionalContainer\\HasConditionalContainer;\n\nclass ExampleNovaResource extends Resource {\n\n    use HasConditionalContainer; // Important!!\n\n    public function fields(Request $request)\n    {\n        return [\n    \n            Select::make('Option', 'option')\n                  -\u003eoptions([\n                      1 =\u003e 'Option 1',\n                      2 =\u003e 'Option 2',\n                      3 =\u003e 'Option 3',\n                  ]),\n    \n            Text::make('Content', 'content')-\u003erules('required'),\n    \n            /**\n             * Only show field Text::make('Field A') if the value of option is equals 1\n             */\n            ConditionalContainer::make([ Text::make('Field A') ])\n                                -\u003eif('option = 1'),\n    \n            /**\n             * Equivalent to: if($option === 2 \u0026\u0026 $content === 'hello world')\n             */\n            ConditionalContainer::make([ Text::make('Field B') ])\n                                -\u003eif('option = 2 AND content = \"hello world\"'),\n    \n            /**\n             * Equivalent to: if(($option !== 2 \u0026\u0026 $content \u003e 10) || $option === 3)\n             */\n            ConditionalContainer::make([ Text::make('Field C')-\u003erules('required') ])\n                                -\u003eif('(option != 2 AND content \u003e 10) OR option = 3'),\n           \n            /**\n             * Example with Validation and nested ConditionalContainer!\n             * Equivalent to: if($option === 3 || $content === 'demo')\n             */\n            ConditionalContainer::make([\n\n                                    Text::make('Field D')-\u003erules('required') // Yeah! validation works flawlessly!!\n                                \n                                    ConditionalContainer::make([ Text::make('Field E') ])\n                                                        -\u003eif('field_d = Nice!')\n                                \n                                ])\n                                -\u003eif('option = 3 OR content = demo')\n        ];\n    }\n\n}\n```\n\nThe `-\u003eif()` method takes a single expression argument that follows this format:\n \n```\n(attribute COMPARATOR value) OPERATOR ...so on\n```\n \nyou can build any complex logical operation by wrapping your condition in `()` examples:\n\n```php\nConditionalContainer::make(...)-\u003eif('first_name = John');\nConditionalContainer::make(...)-\u003eif('(first_name = John AND last_name = Doe) OR (first_name = foo AND NOT last_name = bar)');\nConditionalContainer::make(...)-\u003eif('first_name = John AND last_name = Doe');\n```\n\nyou can chain multiple `-\u003eif()` together to group your expressions by concern, example:\n\n```php\nConditionalContainer::make(...)\n                    //-\u003euseAndOperator()\n                    -\u003eif('age \u003e 18 AND gender = male')\n                    -\u003eif('A contains \"some word\"')\n                    -\u003eif('B contains \"another word\"');\n```\n\nby default the operation applied on each `-\u003eif()` will be `OR`, therefore if any of the if methods evaluates to true the whole \noperation will be considered truthy, if you want to execute an `AND` operation instead append `-\u003euseAndOperator()` to the chain\n\n### Currently supported operators:\n\n- AND\n- OR\n- NOT\n- XOR\n- and parentheses\n\n### Currently supported comparators:\n\n| Comparator          | Description                                    |\n|---------------------|------------------------------------------------|\n| \u003e                   | Greater than                                   |\n| \u003c                   | Less than                                      |\n| \u003c=                  | Less than or equal to                          |\n| \u003e=                  | Greater than or equal to                       |\n| ==                  | Equal                                          |\n| ===                 | Identical                                      |\n| !=                  | Not equal                                      |\n| !==                 | Not Identical                                  |\n| truthy / boolean    | Validate against truthy values                 |\n| contains / includes | Check if input contains certain value          |\n| startsWith          | Check if input starts with certain value       |\n| endsWith            | Check if input ends with certain value         |\n\n#### Examples\n\n* Display field only if user has selected file\n\n```php\n[\n    Image::make('Image'),\n    ConditionalContainer::make([ Text::make('caption')-\u003erules('required') ])\n                        -\u003eif('image truthy true'),\n]\n```\n\n* Display extra fields only if selected morph relation is of type Image or Video\n\n```php\n[\n    MorphTo::make('Resource Type', 'fileable')-\u003etypes([\n        App\\Nova\\Image::class,\n        App\\Nova\\Video::class,\n        App\\Nova\\File::class,\n    ]),\n    \n    ConditionalContainer::make([ Image::make('thumbnail')-\u003erules('required') ])\n                        -\u003eif(function () {\n                            return 'fileable = ' . App\\Nova\\Image::uriKey();\n                        })\n                        -\u003eif(function () {\n                            return 'fileable = ' . App\\Nova\\Video::uriKey();\n                        })\n]\n```\n\n* Display inline HTML only if `Reason` field is empty, show extra fields otherwise.\n\n```php\n[\n    Trix::make('Reason'),\n    \n    ConditionalContainer::make([ Text::make('Extra Information')-\u003erules('required') ])\n                        -\u003eif('reason truthy true'),\n    \n    ConditionalContainer::make([\n                            Heading::make('\u003cp class=\"text-danger\"\u003ePlease write a good reason...\u003c/p\u003e')-\u003easHtml()\n                        ])\n                        -\u003eif('reason truthy false'),\n]\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://raw.githubusercontent.com/dcasia/conditional-container/master/LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcasia%2Fconditional-container","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcasia%2Fconditional-container","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcasia%2Fconditional-container/lists"}