PHPackages                             defstudio/actions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. defstudio/actions

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

defstudio/actions
=================

Helpers methods for Laravel Actions

v2.0.0(3mo ago)119.8k—7.4%MITPHPPHP ^8.2CI failing

Since Nov 23Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/defstudio/actions)[ Packagist](https://packagist.org/packages/defstudio/actions)[ Docs](https://github.com/defstudio/actions)[ GitHub Sponsors](https://github.com/defstudio)[ RSS](/packages/defstudio-actions/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (24)Used By (0)

Helpers methods for Laravel Actions
===================================

[](#helpers-methods-for-laravel-actions)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c4e9ce92f9e97a5434ee73fc06607e09d05b491203810a4433e3eebb9ac70051/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656673747564696f2f616374696f6e732e737667)](https://packagist.org/packages/defstudio/actions)[![GitHub Tests Action Status](https://camo.githubusercontent.com/e854c7eb530989fe8df000fdbb6a3a6dba1b06ea7560e00e9666d15a39ab33a0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6465662d73747564696f2f616374696f6e732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)](https://github.com/def-studio/actions/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/c4c9d55026ba1d41364d458dc71791416d461408d076c8e324128376dc8de1e5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6465662d73747564696f2f616374696f6e732f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65)](https://github.com/def-studio/actions/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![GitHub Static Analysis Action Status](https://camo.githubusercontent.com/712af1baf3f82e67f7a4f335995ce5a4a89cec6fc98dba7071c2259c63ba1fe1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6465662d73747564696f2f616374696f6e732f7068707374616e2e796d6c3f6272616e63683d6d61696e266c6162656c3d7068707374616e)](https://github.com/def-studio/actions/actions?query=workflow%3Aphpstan+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/a290078be6b3644ab1fcba1847d929e17d0e9a1f0fd439a53b967505e7ddb77e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64656673747564696f2f616374696f6e732e737667)](https://packagist.org/packages/defstudio/actions)

an opinionated lightweight package for creating Action classes

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require defstudio/actions
```

Usage
-----

[](#usage)

Add `use DefStudio\Actions\ActAsAction;` trait to your action class or extend `DefStudio\Actions\Action`

(optional) add a dockblock to hint the static `run` method parameters and return types

```
/**
 * @method static void run(Report|int $report)
 */
class DeleteReport
{
    use ActsAsAction;

    public function handle(Report|int $report): void
    {
        if (is_int($report)) {
            $report = Report::findOrFail($report);
        }

        DB::transaction(function () use ($report) {
            $report->delete_data();
            $report->delete();
        });
    }
}

class DeleteReport extends \DefStudio\Actions\Action
{
    public function handle(Report|int $report): bool
    {
        if (is_int($report)) {
            $report = Report::findOrFail($report);
        }

        return DB::transaction(function () use ($report) {
            $report->delete_data();
            return $report->delete();
        });
    }
}
```

Use the new methods:

```
$result = DeleteReport::run($report->id); //true

$result = DeleteReport::make()->handle($report->id); //true
```

Run multiple actions as once
----------------------------

[](#run-multiple-actions-as-once)

an action can be run multiple times by calling its `runMany()` method

```
$results = DeleteReport::runMany($report1->id, $report2->id, $report3->id); //[true, false, true]
```

each run's result will be collected in an array and returned by `runMany()`

*notes*

if multiple parameters are required, they can be wrapped in an array (associative array keys will be treated as named arguments):

```
class{
    use InjectsItself;

    public function handle($name = 'guest', $title = 'Mr.'): string
    {
        return "$title $name";
    }
}

$result = MyAwesomeAction::runMany(['Elizabeth', "Ms."], ['Fabio'],  ['title' => 'Mrs.']);

// $result = ["Ms. Elizabeth", "Mr. Fabio", "Mrs. guest"]
```

Mockable actions
----------------

[](#mockable-actions)

Also, you can define a mock for the action (it will be authomatically bound to the app container):

```
FindTheAnswerToLifeTheUniverseAndEverything::mock(fn ($report_id) => 42);

FindTheAnswerToLifeTheUniverseAndEverything::run() // 42
```

if you are interested in only mocking the return value, you can write:

```
FindTheAnswerToLifeTheUniverseAndEverything::mock(42);
```

if your action has public methods other than `handle`, they can be mocked as well:

```
MyWeirdAction::mock(handle: fn() => 5, handleForAdmin: fn() => 42);
```

without arguments, `mocks` returns a MockInterface instance ready to be used

```
MyAction::mock()->shouldNotReceive('handle');
```

a partial mock (i.e. for actions with more than a single method)

```
BuildOrder::partial_mock(fromRequest: fn() => true);

//this will not be mocked
BuildOrder::make()->fromJson($data);
```

along with mocks, actions can also be *spied*:

```
$spiedAction = MyAction::spy();

$spiedAction->handle();
$spiedAction->handle();

$spiedAction->shouldHaveReceived()->handle()->twice()
```

Dispatchable actions
--------------------

[](#dispatchable-actions)

An action can be made *dispatchable* as a job with the `ActsAsJob` trait (or extending the `Action` class)

a job can be created by calling the `job()` static method:

```
dispatch(LongRunningAction::job($argument_1, $argument_2));
```

or can be dispatched with its dedicated methods:

```
LongRunningAction::dispatch($argument_1, $argument_2);

LongRunningAction::dispatchSync($argument_1, $argument_2);

LongRunningAction::dispatchAfterResponse($argument_1, $argument_2);
```

The action will be dispatched wrapped in a ActionJob decorator that will proxy properties as needed:

```
use DefStudio\Actions\Concerns\ActsAsJob;

class LongRunningAction{
    use ActsAsJob;

    public int $timeout = 2 * 60 * 60;
    public int $tries = 4;
    public array $backoff = [60, 120, 300, 600];
    public string $queue = 'long-running';

    public function handle(){...}
}
```

### Cleaning up after failed action job

[](#cleaning-up-after-failed-action-job)

Failed action jobs can be handled by defining a `jobFailed()` method:

```
class LongRunningAction{
    use ActsAsJob;

    public function handle(){..}

    public function jobFailed($exception)
    {
        $this->handleFailure();
    }

    private function handleFailure(){..}
}
```

### Batches and Chains of action jobs

[](#batches-and-chains-of-action-jobs)

Similarly to the `runMany()` method, a new batch/chain of action jobs can be created starting from an array of parameters:

```
MyAction::batch([$name1, $title1], [$name2, $title2])->dispatch();

MyAction::chain([$name1, $title1], [$name2, $title2])->dispatch();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Fabio Ivona](https://github.com/fabio-ivona)
- [All Contributors](../../contributors)

This project was inspired and is built as an opinionated and simplified implementation of [Loris Leiva](https://github.com/lorisleiva)'s [Laravel Actions](https://laravelactions.com/). For a more powerful tool, you should take a look at it.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance80

Actively maintained with recent releases

Popularity29

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 88.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~76 days

Recently: every ~264 days

Total

21

Last Release

105d ago

Major Versions

v0.1.0 → v1.0.02022-02-11

v1.1.4 → v2.0.02026-02-03

PHP version history (2 changes)v0.0.1PHP ^8.0

v2.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/db5e0ab5568062368a52c61d67381c1a35be5e5c816968dd3883bc7ba2d46b53?d=identicon)[fabio.ivona](/maintainers/fabio.ivona)

---

Top Contributors

[![fabio-ivona](https://avatars.githubusercontent.com/u/8792274?v=4)](https://github.com/fabio-ivona "fabio-ivona (110 commits)")[![MarioGattolla](https://avatars.githubusercontent.com/u/94918437?v=4)](https://github.com/MarioGattolla "MarioGattolla (14 commits)")

---

Tags

laravelactionsdefstudio

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/defstudio-actions/health.svg)

```
[![Health](https://phpackages.com/badges/defstudio-actions/health.svg)](https://phpackages.com/packages/defstudio-actions)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
