Allow actions in your laravel model

1.3.2 2021-04-10 15:02 UTC

This package is auto-updated.

Last update: 2022-06-10 17:42:14 UTC


Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Allow models to perform or receive actions.


You can install the package via composer:

composer require narcisonunez/laravel-actionable-model

You can publish and run the migrations with:

php artisan vendor:publish --provider="Narcisonunez\LaravelActionableModel\LaravelActionableModelServiceProvider" --tag="actionable-model-migrations"
php artisan migrate

Basic Setup

Register your actions

Add your action in your AppServiceProvider

use Narcisonunez\LaravelActionableModel\Facades\ActionableActionTypes;
use App\ActionTypes\KudosActionType;

    'kudos' => KudosActionType::class,

You can implement your own class extending ActionableTypeRecord to add more logic to your records.
Ex. icon method to get that specific action type icon. Now, Any kudos action will have a method named icon.

Your actions will be used as dynamic method calls. See below.

Create a new Actionable Action Type (OPTIONAL)

php artisan actionable:type LikeActionType

Add aliases to your models (OPTIONAL)

Add your aliases in your AppServiceProvider

use App\Models\Cause;
use App\Models\User;
use Narcisonunez\LaravelActionableModel\Facades\ActionableModelAliases;

    User::class => 'user',
    Cause::class => 'cause'

Storing aliases in the database will prevent losing the reference if you move your models to another directory.

Update existing models references to use the new alias

In case you already have data in the database, after adding the aliases you can run:

php artisan actionable:update-aliases // Update all the records

If you want just to update a specific value

php artisan actionable:update-aliases --from="App\\User" --to="App\\Models\\User"

To update all your existing records.


A model that can perform actions you need to include:

// Imports 
class User extends Authenticatable
    use ActionableModel;
    use CanPerformActions;

The model that can receive the actions must implement CanBeActionable

use Narcisonunez\LaravelActionableModel\Traits\ActionableModel;

class Cause extends Model implements CanBeActionable
    use ActionableModel;

Basic Usage

Perform an action

// You will use your actions as methods call.

Check if the action was already made

// returns False or an ActionableTypeRecord

Toggle your actions

// remove if exists the action, otherwise creates a new like
// OR

Manually delete an action (See Toggle above)

if ($action  = $user->hasPerformedAction('like')->on($cause) ) {

Get all the actions


// To help you out filtering your actions. You can use the actionsFilter method

- Available methods -

Method Description
get Returns a collection of ActionableTypeRecord
count Returns the number of records
given Filters all the records where the current model performed the actions
received Filters all the records where the current model received the actions
ofType Filters by the actionType
by Filters all the actions in the current model where the model passed to this method performed the action
latest Get the collection sorted by the latest ones.

Actionable Record

The methods above will return a collection of ActionableRecord.
The access the owner or the actionable models, you can do it like this:

$actionRecord = $user->actionsFilter()->ofType('like')->get()->first();

$actionRecord->owner; // The model that performed the action
$actionRecord->actionable; // The model that received the action
$actionRecord->action; // Action that was performed. ex. like, kudos, celebrate, etc.
$actionRecord->type; // An alias to action

💖 Support the development

Do you like this project? Support it by donating


The MIT License (MIT). Please see License File for more information.