# Events

Laratrust comes with an events system that works like the Laravel model events. The events that you can listen to are roleAdded, roleRemoved, permissionAdded, permissionRemoved, roleSynced, permissionSynced.

NOTE

Inside the Role model only the permissionAdded, permissionRemoved and permissionSynced events will be fired.

If you want to listen to a Laratrust event, inside your User or Role models put this:

<?php

namespace App;

use Laratrust\Traits\HasRolesAndPermissions;

class User extends Model
{
    use HasRolesAndPermissions;

    public static function boot() {
        parent::boot();

        static::roleAdded(function($user, $role, $team) {
        });
        static::roleSynced(function($user, $changes, $team) {
        });
    }
}

NOTE

The $team parameter is optional and if you are not using teams, it will be set to null.

The eventing system also supports observable classes:

<?php

namespace App\Observers;

use App\User;

class UserObserver
{

    public function roleAdded(User $user, $role, $team)
    {
        //
    }

    public function roleSynced(User $user, $changes, $team)
    {
        //
    }
}

IMPORTANT

To register an observer, use the laratrustObserve method on the model you wish to observe.

You may register observers in the boot method of one of your service providers. In this example, we'll register the observer in the AppServiceProvider:

<?php

namespace App\Providers;

use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    public function boot()
    {
        User::laratrustObserve(UserObserver::class);
    }

    ...
}

NOTE

  • Inside your observable classes you can have your normal model events methods alongside Laratrust's events methods.
  • If you want to register Laratrust events and also eloquent events you should call both observe and laratrustObserve methods.

# Flushing events and observables

If you want to flush the observables and events from laratrust you should add the following in your code:

User::laratrustFlushObservables();
User::flushEventListeners();

# Available Events

# User Events

  • roleAdded($user, $role, $team = null)

    • $user: The user to whom the role was added.
    • $role: The role id that was added to the $user.
    • $team: The team id that was used to add the role to the $user.
  • roleRemoved($user, $role, $team = null)

    • $user: The user to whom the role was detached.
    • $role: The role id that was detached from the $user.
    • $team: The team id that was used to detach the role from the $user.
  • permissionAdded($user, $permission, $team = null)

    • $user: The user to whom the permission was added.
    • $permission: The permission id that was added to the $user.
    • $team: The team id that was used to add the permission to the $user.
  • permissionRemoved($user, $permission, $team = null)

    • $user: The user to whom the permission was detached.
    • $permission: The permission id that was detached from the $user.
    • $team: The team id that was used to detach the permission from the $user.
  • roleSynced($user, $changes, $team)

    • $user: The user to whom the roles were synced.
    • $changes: The value returned by the eloquent sync method containing the changes made in the database.
    • $team: The team id that was used to sync the roles to the user.
  • permissionSynced()

    • $user: The user to whom the permissions were synced.
    • $changes: The value returned by the eloquent sync method containing the changes made in the database.
    • $team: The team id that was used to sync the permissions to the user.

# Role Events

  • permissionAdded($role, $permission)

    • $role: The role to whom the permission was added.
    • $permission: The permission id that was added to the $role.
  • permissionRemoved($role, $permission)

    • $role: The role to whom the permission was detached.
    • $permission: The permission id that was detached from the $role.
  • permissionSynced()

    • $role: The role to whom the permissions were synced.
    • $changes: The value returned by the eloquent sync method containing the changes made in the database.