{"id":18941037,"url":"https://github.com/cleaniquecoders/laravel-action","last_synced_at":"2025-04-15T19:32:01.120Z","repository":{"id":59053724,"uuid":"535163249","full_name":"cleaniquecoders/laravel-action","owner":"cleaniquecoders","description":"Simple Actionable for Laravel ","archived":false,"fork":false,"pushed_at":"2025-02-03T03:37:24.000Z","size":93,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-13T22:04:27.760Z","etag":null,"topics":["action","laravel","php"],"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/cleaniquecoders.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-11T01:41:36.000Z","updated_at":"2025-02-03T03:37:22.000Z","dependencies_parsed_at":"2023-12-03T07:35:42.715Z","dependency_job_id":"5cf470d7-c3ac-4b78-aa19-b9e5cb2e3978","html_url":"https://github.com/cleaniquecoders/laravel-action","commit_stats":null,"previous_names":["cleaniquecoders/laravel-action","bekwoh/laravel-action"],"tags_count":12,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleaniquecoders%2Flaravel-action","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleaniquecoders%2Flaravel-action/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleaniquecoders%2Flaravel-action/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cleaniquecoders%2Flaravel-action/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cleaniquecoders","download_url":"https://codeload.github.com/cleaniquecoders/laravel-action/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249138859,"owners_count":21218961,"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":["action","laravel","php"],"created_at":"2024-11-08T12:25:40.382Z","updated_at":"2025-04-15T19:32:00.840Z","avatar_url":"https://github.com/cleaniquecoders.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Actionable for Laravel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/cleaniquecoders/laravel-action.svg?style=flat-square)](https://packagist.org/packages/cleaniquecoders/laravel-action) [![PHPStan](https://github.com/cleaniquecoders/laravel-action/actions/workflows/phpstan.yml/badge.svg)](https://github.com/cleaniquecoders/laravel-action/actions/workflows/phpstan.yml) [![run-tests](https://github.com/cleaniquecoders/laravel-action/actions/workflows/run-tests.yml/badge.svg)](https://github.com/cleaniquecoders/laravel-action/actions/workflows/run-tests.yml) [![Fix PHP code style issues](https://github.com/cleaniquecoders/laravel-action/actions/workflows/fix-styling.yml/badge.svg)](https://github.com/cleaniquecoders/laravel-action/actions/workflows/fix-styling.yml) [![Total Downloads](https://img.shields.io/packagist/dt/cleaniquecoders/laravel-action.svg?style=flat-square)](https://packagist.org/packages/cleaniquecoders/laravel-action)\n\nThis package, **Simple Actionable for Laravel**, provides a structured way to manage action classes in your Laravel applications, making it easier to encapsulate business logic, transformations, and validations within reusable classes. The package utilizes [lorisleiva/laravel-actions](https://github.com/lorisleiva/laravel-actions) to offer extended functionality, enabling actions to be executed in multiple contexts (e.g., jobs, controllers, event listeners) and simplifying your codebase.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require cleaniquecoders/laravel-action\n```\n\n## Features\n\nThis package builds on top of `lorisleiva/laravel-actions`, allowing you to:\n- Create versatile action classes that can be executed as invokable objects, controllers, or dispatched as jobs.\n- Use property setters to configure actions dynamically.\n- Apply transformations like hashing or encryption to specific fields.\n- Manage `updateOrCreate` behavior with constraints on unique fields.\n\n## Usage\n\nYou can create an action using the Artisan command:\n\n```bash\nphp artisan make:action User\\\\CreateOrUpdateUser --model=User\n```\n\nThis will create an action in `app\\Actions\\User`:\n\n```php\n\u003c?php\n\nnamespace App\\Actions\\User;\n\nuse App\\Models\\User;\nuse CleaniqueCoders\\LaravelAction\\ResourceAction;\n\nclass CreateOrUpdateUser extends ResourceAction\n{\n    public string $model = User::class;\n\n    public function rules(): array\n    {\n        return [\n            'name' =\u003e 'required|string|max:255',\n            'email' =\u003e 'required|string|email|unique:users,email',\n            'password' =\u003e 'required|string|min:8',\n        ];\n    }\n}\n```\n\n### New Features and Example Usage\n\n#### 1. Flexible Property Setter\n\nYou can set properties, such as `hashFields`, `encryptFields`, and `constrainedBy`, dynamically using the `setProperty` method:\n\n```php\n$action = new CreateOrUpdateUser(['name' =\u003e 'John Doe', 'email' =\u003e 'johndoe@example.com', 'password' =\u003e 'secretpassword']);\n$action-\u003esetProperty('hashFields', ['password']); // Hash the password\n$action-\u003esetProperty('encryptFields', ['ssn']); // Encrypt SSN\n$action-\u003esetProperty('constrainedBy', ['email' =\u003e 'johndoe@example.com']); // Use email as a unique constraint\n```\n\nThis flexible property setting reduces boilerplate and simplifies action configuration.\n\n#### 2. Field Transformation with Hashing and Encryption\n\nThe `ResourceAction` class supports field-level transformations. For example, you can hash a `password` field and encrypt an `ssn` field:\n\n```php\n$inputs = [\n    'name' =\u003e 'Jane Doe',\n    'email' =\u003e 'janedoe@example.com',\n    'password' =\u003e 'securepassword',\n    'ssn' =\u003e '123-45-6789',\n];\n\n$action = new CreateOrUpdateUser($inputs);\n$action-\u003esetProperty('hashFields', ['password']);\n$action-\u003esetProperty('encryptFields', ['ssn']);\n$record = $action-\u003ehandle();\n```\n\nAfter execution:\n- The `password` field will be hashed for security.\n- The `ssn` field will be encrypted, ensuring secure storage.\n\n#### 3. Constraint-Based `updateOrCreate`\n\nSpecify constraints to perform `updateOrCreate` actions based on unique fields or identifiers. Here’s an example of updating an existing user by `id`:\n\n```php\n// Assume there's an existing user with this email\n$existingUser = User::create([\n    'name' =\u003e 'Old Name',\n    'email' =\u003e 'uniqueemail@example.com',\n    'password' =\u003e Hash::make('oldpassword'),\n]);\n\n// Define the inputs to update the existing user\n$inputs = [\n    'name' =\u003e 'John Doe Updated',\n    'email' =\u003e 'uniqueemail@example.com', // Same email\n    'password' =\u003e 'newpassword',\n];\n\n$action = new CreateOrUpdateUser($inputs);\n$action-\u003esetProperty('constrainedBy', ['id' =\u003e $existingUser-\u003eid]); // Update by user ID\n\n$record = $action-\u003ehandle();\n\n// The existing user record with the specified ID will be updated.\n```\n\nThis allows precise control over `updateOrCreate` behavior based on custom constraints.\n\n## Using `lorisleiva/laravel-actions` for Multi-Context Execution\n\nWith `lorisleiva/laravel-actions`, actions created with this package can be used in multiple contexts. You can run the action as:\n- **An Invokable Object**:\n  ```php\n  $user = (new CreateOrUpdateUser(['name' =\u003e 'Jane', 'email' =\u003e 'jane@example.com']))-\u003ehandle();\n  ```\n- **A Controller**:\n  ```php\n  Route::post('users', CreateOrUpdateUser::class);\n  ```\n- **A Job**:\n  ```php\n  CreateOrUpdateUser::dispatch(['name' =\u003e 'Jane', 'email' =\u003e 'jane@example.com']);\n  ```\n- **An Event Listener**:\n  ```php\n  Event::listen(UserRegistered::class, CreateOrUpdateUser::class);\n  ```\n\n## Testing\n\nRun the tests with:\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on recent changes.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Nasrul Hazim Bin Mohamad](https://github.com/nasrulhazim)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcleaniquecoders%2Flaravel-action","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcleaniquecoders%2Flaravel-action","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcleaniquecoders%2Flaravel-action/lists"}