laravel-actions maintained by panchodp
Laravel Actions
Make your Laravel actions classes fast and in a simple way.
Compatibility
| Laravel | PHP | Package |
|---|---|---|
| 13.x | 8.4+ | ^3.x |
| 12.x | 8.3+ | ^2.x |
Upgrading from 2.x: version 3.0 drops PHP 8.3 and Laravel 12. If you are still on either, stay on
^2.5.
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.
Available placeholders:
| Placeholder | Replaced with |
|---|---|
{{ namespace }} |
Namespace of the action (e.g. App\Actions\User) |
{{ root_namespace }} |
Root namespace of your application, with trailing backslash (e.g. App\) |
{{ class }} |
Action class name |
{{ method }} |
Method name (method_name config) |
{{ method_type }} |
static when --static is used, empty otherwise |
{{ import_model }} |
use {root namespace}Models\User; when --user is used, empty otherwise |
{{ user }} |
User $user, when --user is used, empty otherwise |
{{ request_class }} |
Request class name (e.g. CreateUserRequest) when --request is used |
Upgrading published stubs: generated imports now use your application's root namespace instead of a hardcoded
App\. If you published the stubs with an earlier version and your app does not use theApp\namespace, re-publish them with--forceor replaceApp\Http\Requests\with{{ root_namespace }}Http\Requests\.
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.
Recommendation: Prefer instance methods (the default) and resolve them from the container with
app(MyAction::class)->handle(...). Instance methods support constructor dependency injection, are easier to mock and test, and can implement contracts. Reserve--static/-sfor genuinely stateless, dependency-free helpers such as a pure calculation.
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 or User/CreateUser │
└────────────────────────────────────────────────────────────┘
┌ Subfolder (optional) ──────────────────────────────────────┐
│ e.g. User or User/Auth │
└────────────────────────────────────────────────────────────┘
┌ Include DB transaction? ───────────────────────────────────┐
│ ● Yes / ○ No │
└────────────────────────────────────────────────────────────┘
...
The action name accepts the same path syntax as the command line (User/Auth/Login). Flags passed on the command line are applied without being asked again, so php artisan make:action -t only prompts for the remaining options. --force is honored as well.
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.
Passing the subfolder as a second argument:
php artisan make:action CreateAccount User/Profile
This creates the action in app/Actions/User/Profile/CreateAccount.php.
Folder names must start with a letter or underscore and contain only letters, numbers and underscores. Spaces are not allowed inside a folder name ("User Auth" is rejected instead of being turned into User/Auth).
The generated class will have a handle method where you can implement your action logic:
<?php
declare(strict_types=1);
namespace App\Actions\User;
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 Useful 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
Laravel Boost
This package ships an Agent Skill and a guideline for Laravel Boost. If your project uses Boost, your AI agent (Claude Code, Cursor, Copilot, etc.) can learn how to scaffold actions correctly with make:action.
The skill is not copied automatically when you install or update this package — Boost only writes skills to your agents when you run one of its commands and the package is selected in your boost.json:
# Fresh setup: select panchodp/laravel-actions in the package list
php artisan boost:install
# Already using Boost? Use --discover so Boost finds this newly added
# package and prompts you to add it (a plain `boost:update` won't).
php artisan boost:update --discover
When prompted "New packages with guidelines/skills discovered!", make sure to select
panchodp/laravel-actions. As a third-party package it must be listed underpackagesinboost.json; only then does Boost copy thelaravel-actions-developmentskill into the directories of every agent you have configured (.claude/skills/,.cursor/, etc.) and merge the guideline intoAGENTS.md/CLAUDE.md.
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.