PHPackages                             uteq/laravel-model-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. uteq/laravel-model-actions

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

uteq/laravel-model-actions
==========================

Magically adds actions to a model

1.2.0(5y ago)10379[2 issues](https://github.com/uteq/laravel-model-actions/issues)MITPHPPHP ^8.0

Since Mar 9Pushed 3y agoCompare

[ Source](https://github.com/uteq/laravel-model-actions)[ Packagist](https://packagist.org/packages/uteq/laravel-model-actions)[ Docs](https://github.com/uteq/laravel-model-actions)[ GitHub Sponsors](https://github.com/uteq)[ RSS](/packages/uteq-laravel-model-actions/feed)WikiDiscussions master Synced yesterday

READMEChangelog (5)Dependencies (6)Versions (8)Used By (0)

Magically adds actions to a model
=================================

[](#magically-adds-actions-to-a-model)

[![Latest Version on Packagist](https://camo.githubusercontent.com/15c46aa02ad961b3320f7865cbd4530107659c785d135018fcec5d5709fa3d91/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f757465712f6c61726176656c2d6d6f64656c2d616374696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/uteq/laravel-model-actions)[![GitHub Tests Action Status](https://camo.githubusercontent.com/52764fe03c8442347b16e0b088081913758c44405a9b5df68dc2a41692c8e190/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f757465712f6c61726176656c2d6d6f64656c2d616374696f6e732f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/uteq/laravel-model-actions/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/a733c65b6bbcce4182fd75a9108b7269dffdf5bb4a09a8307d5517aae24f8299/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f757465712f6c61726176656c2d6d6f64656c2d616374696f6e732f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/uteq/laravel-model-actions/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/73e4c02675e25196864d2b632fc3260711976a972aa871de297788b3e1ebe8b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f757465712f6c61726176656c2d6d6f64656c2d616374696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/uteq/laravel-model-actions)

This package will magically add actions to a model. Simply adding the WithActions trait:

Add the trait to your model

```
use Uteq\ModelActions\Concerns\WithActions;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use WithActions;
}
```

And than use it in all sorts of ways:

```
$user->action()->update($input);
$user->action('update', $input);
$user->action(\App\Actions\User\Update::class, $input);
```

Whenever you need to call a method on a model that does not (yet) have an active record in the database, you can also use this static method.

```
User::do(Create::class, $input);
```

This package was inspired by this read about OOP: Especially the last part about actions being added to a model made sense to me. This will keep your models clean, and your actions separated.

The idea behind this package is that adding actions makes your application more scalable. 'Grouping' these actions by adding them to a model, makes it easier to comprehend where the action is for. It is more declarative having your model $user-&gt;action() perform an action than having the action out of the blue. To create more context you would have to always prefix your action UserCreateAction for example.

Not convinced about using Actions in your application? Read this excellent blog post of Brent from Spatie.be

You will need php 8.0

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

[](#installation)

You can install the package via composer:

```
composer require uteq/laravel-model-actions
```

You can publish the config file with:

```
php artisan vendor:publish --provider="Uteq\ModelActions\ModelActionsServiceProvider" --tag="laravel-model-actions-config"
```

This is the contents of the published config file:

```
return [
    /**
     * You can overwrite the method used to handle the
     * action. By default this is __invoke.
     */
    'method' => '__invoke',
];
```

Usage
-----

[](#usage)

### On your model

[](#on-your-model)

Add the WithActions trait to the model

```
use Uteq\ModelActions\Concerns\WithActions;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use WithActions;
}
```

### Directory

[](#directory)

The actions should be added in the following folder structure

```
App
├── Actions
│   └── User
│       ├── Create.php
│       ├── Update.php
│       ├── Destroy.php
│       └── AddImage.php
└── Models
    └── User.php

```

After that you can always access the actions from your model:

This is how an action class looks like:

```
class Update
{
    public function __invoke(User $model, array $input = [])
    {
        // Now add you own logic here
    }
}
```

As you can see the $user will automatically be injected into the \_\_invoke method. The system knows the user because you are calling in from the user. Please note that the model is always the first parameter.

The name of the Action class will be used as the method name. So a class UpdateImage will be accessible using User::action()-&gt;updateImage($input);

```
$user->action(Update::class, $input);
```

### Dependency injection in Actions

[](#dependency-injection-in-actions)

Dependency injection in the \_\_construct of the action is by default. So you can do this:

```
class Destroy
{
    public function __construct(
        protected PublicDestroyer $destroyer,
    ) { }

    public __invoke(User $user, array $input = [])
    {
        ($this->destroyer)($user, $input);
    }
}
```

### Parameter binding

[](#parameter-binding)

Parameter binding for model actions is pretty straight forward. We have Named parameters (see below) and simply using the given order:

```
class Action
{
    public function __invoke($var1, $var2)
    {

    }
}
```

```
User::do(Action::class, 'var1', 'var2');
```

### Named parameters

[](#named-parameters)

Utilizing php 8's named paramters you are able to be very strict into what your action class accepts.

```
class Action
{
    public function __invoke($name)
    {

    }
}
```

```
User::do(Action::class, name: 'test');
```

Examples
--------

[](#examples)

### Apply with actions to all your models

[](#apply-with-actions-to-all-your-models)

A convenient way to add the WithActions to all your Models is by simply extending the Eloquent Model class and extend upon that class.

```
namespace App\Support;

use Uteq\ModelActions\Concerns\WithActions;

class Model extends \Illuminate\Database\Eloquent\Model
{
    use WithActions;
}
```

```
class ActionModel extends \App\Support\Model
{

}
```

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)

- [Nathan Jansen](https://github.com/nathanjansen)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~102 days

Recently: every ~127 days

Total

6

Last Release

1378d ago

Major Versions

0.0.1 → 1.0.02021-03-09

1.2.0 → v2.0.x-dev2022-08-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/1e9390c24b53926dfe0f57b44d2b09cce785f2295dadee3654d940d71cd3ec20?d=identicon)[uteq](/maintainers/uteq)

---

Top Contributors

[![nathanjansen](https://avatars.githubusercontent.com/u/73473323?v=4)](https://github.com/nathanjansen "nathanjansen (28 commits)")

---

Tags

uteqlaravel-model-actions

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/uteq-laravel-model-actions/health.svg)

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

###  Alternatives

[guava/calendar

Adds support for vkurko/calendar to Filament PHP.

298241.0k3](/packages/guava-calendar)[tonysm/rich-text-laravel

Integrates Trix content with Laravel

46577.8k1](/packages/tonysm-rich-text-laravel)[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)[tonysm/globalid-laravel

Identify app models with a URI. Inspired by the globalid gem.

45101.6k2](/packages/tonysm-globalid-laravel)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[spatie/laravel-screenshot

Take screenshots of web pages in Laravel apps

7615.9k2](/packages/spatie-laravel-screenshot)

PHPackages © 2026

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