panchodp / laravel-actions
Make your Laravel actions classes fast and in a simple way.
Requires
- php: ^8.3
Requires (Dev)
- larastan/larastan: ^3.9.2
- laravel/pint: ^1.27.1
- orchestra/testbench: ^10.9
- pestphp/pest: ^3.8.5
- rector/rector: ^2.3
README
Laravel Actions
Make your Laravel actions classes fast and in a simple way.
Installation
You can install the package via composer:
composer require panchodp/laravel-actions --dev
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="Panchodp\LaravelAction\LaravelActionServiceProvider" --tag="laravel-actions-config"
This creates config/laravel-actions.php:
return [ 'base_folder' => 'Actions', 'method_name' => 'handle', ];
base_folder: Base folder where action classes are created. Defaults toActions(app/Actions).method_name: Method name generated in action classes. Defaults tohandle.
Customizing Stubs
You can publish and edit the stub templates used to generate action classes:
php artisan vendor:publish --provider="Panchodp\LaravelAction\LaravelActionServiceProvider" --tag="laravel-actions-stubs"
This publishes the 4 stubs to resources/stubs/vendor/laravel-actions/. Once published, the package will use your custom stubs instead of the defaults. You can customize each stub independently — any stub not found in your published directory will fall back to the package default.
Method Types
By default, Laravel Actions generates instance methods for better flexibility and dependency injection support. However, you can create static methods when needed for simpler usage.
Instance Methods (Default)
// Usage $action = new MyAction(); $action->handle($attributes); // Generated code public function handle(array $attributes): void { // Implementation }
Static Methods
// Usage MyAction::handle($attributes); // Generated code public static function handle(array $attributes): void { // Implementation }
Usage
Interactive Mode
Run make:action without arguments to launch an interactive wizard:
php artisan make:action
┌ Action name ───────────────────────────────────────────────┐
│ e.g. CreateUser │
└────────────────────────────────────────────────────────────┘
┌ Subfolder (optional) ──────────────────────────────────────┐
│ e.g. User or User/Auth │
└────────────────────────────────────────────────────────────┘
┌ Include DB transaction? ───────────────────────────────────┐
│ ● Yes / ○ No │
└────────────────────────────────────────────────────────────┘
...
Creating Actions
To create an action class, use the make:action command. You can specify the full path using forward slashes / or backslashes \ (Laravel-style syntax):
Basic action:
php artisan make:action MyAction
This creates a new action class in the app/Actions directory.
Action in a subfolder:
php artisan make:action User/CreateAccount
This creates the action in app/Actions/User/CreateAccount.php.
Action in nested subfolders:
php artisan make:action User/Auth/Login
This creates the action in app/Actions/User/Auth/Login.php.
Using backslashes (alternative syntax):
php artisan make:action Admin\DeletePost
This creates the action in app/Actions/Admin/DeletePost.php.
The generated class will have a handle method where you can implement your action logic:
<?php declare(strict_types=1); namespace App\Actions\User; use Throwable; final class CreateAccount { public function handle(array $attributes): void { // This is where the action logic will be implemented. } }
Flags
| Flag | Shortcut | Description |
|---|---|---|
--transaction |
-t |
Wraps the action body in a DB::transaction |
--user |
-u |
Injects User $user into the method |
--request |
-r |
Generates a Request class and injects it into the method |
--static |
-s |
Generates a static method instead of an instance method |
--force |
Overwrites the action if it already exists |
Shortcuts can be combined in any order, just like make:model:
php artisan make:action MyAction -tur # transaction + user + request php artisan make:action MyAction -turs # + static method # Long form works too php artisan make:action MyAction --transaction --user --request
Example output for --turs:
final class MyAction { public static function handle(User $user, MyActionRequest $request): void { DB::transaction(function () use ($request) { // Logic to be executed within the transaction }); } }
Other Userfull Commands:
You can show the Actions directory tree in the terminal with the following command:
php artisan actions:list
This will display the structure of the app/Actions or the base directory specified in the config file, showing all action classes and their subdirectories.
Actions/ ├── Folder1/ │ ├── SecondAction │ └── ThirdAction ├── Folder2/ │ └── FourthAction ├── FirstAction └── LastAction
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
