Download the PHP package uteq/laravel-model-actions without Composer
On this page you can find all versions of the php package uteq/laravel-model-actions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download uteq/laravel-model-actions
More information about uteq/laravel-model-actions
Files in uteq/laravel-model-actions
Package laravel-model-actions
Short Description Magically adds actions to a model
License MIT
Homepage https://github.com/uteq/laravel-model-actions
Informations about the package laravel-model-actions
Magically adds actions to a model
This package will magically add actions to a model. Simply adding the WithActions trait:
Add the trait to your model
And than use it in all sorts of ways:
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.
This package was inspired by this read about OOP: https://www.tonysm.com/when-objects-are-not-enough/#objects-in-the-large 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->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 https://stitcher.io/blog/laravel-beyond-crud-03-actions
You will need php 8.0
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
Usage
On your model
Add the WithActions trait to the model
Directory
The actions should be added in the following folder structure
After that you can always access the actions from your model:
This is how an action class looks like:
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()->updateImage($input);
Dependency injection in Actions
Dependency injection in the __construct of the action is by default. So you can do this:
Parameter binding
Parameter binding for model actions is pretty straight forward. We have Named parameters (see below) and simply using the given order:
Named parameters
Utilizing php 8's named paramters you are able to be very strict into what your action class accepts.
Examples
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.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Nathan Jansen
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-model-actions with dependencies
illuminate/contracts Version ^8.0
spatie/laravel-package-tools Version ^1.1