All Projects → alexstewartja → laravel-attribute-observer

alexstewartja / laravel-attribute-observer

Licence: MIT license
Observe (and react to) attribute changes made on Eloquent models.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-attribute-observer

Laravel Auditing
Record the change log from models in Laravel
Stars: ✭ 2,210 (+3645.76%)
Mutual labels:  eloquent, observer
attribute-events
🔥 Fire events on attribute changes of your Eloquent model
Stars: ✭ 198 (+235.59%)
Mutual labels:  events, eloquent
EventEmitter
Simple EventEmitter with multiple listeners
Stars: ✭ 19 (-67.8%)
Mutual labels:  events, observer
Observable
minimalist event system for Python
Stars: ✭ 66 (+11.86%)
Mutual labels:  events, observer
go-observer
Go package for simplifying channel-based broadcasting of events from multiple publishers to multiple observers
Stars: ✭ 66 (+11.86%)
Mutual labels:  events, observer
Ease
It's magic.
Stars: ✭ 1,213 (+1955.93%)
Mutual labels:  events, observer
Pevents
Implementation of Win32 events for *nix platforms, built on top of pthreads.
Stars: ✭ 200 (+238.98%)
Mutual labels:  events
Cphalcon7
Dao7 - Web framework for PHP7.x,项目接洽 QQ 176013762
Stars: ✭ 237 (+301.69%)
Mutual labels:  events
Library
This is a project of a library, driven by real business requirements. We use techniques strongly connected with Domain Driven Design, Behavior-Driven Development, Event Storming, User Story Mapping.
Stars: ✭ 2,685 (+4450.85%)
Mutual labels:  events
Ember Native Dom Helpers
Test helpers for your integration tests that fire native events
Stars: ✭ 187 (+216.95%)
Mutual labels:  events
penn-clubs
Official React-based website for Penn Labs' club directory and events listings.
Stars: ✭ 41 (-30.51%)
Mutual labels:  events
micro-typed-events
The smallest, most convenient typesafe TS event emitter you'll ever need
Stars: ✭ 39 (-33.9%)
Mutual labels:  events
React Native Add Calendar Event
Create, view or edit events in react native using the standard iOS / Android dialogs
Stars: ✭ 225 (+281.36%)
Mutual labels:  events
Luv
Cross-platform asynchronous I/O and system calls
Stars: ✭ 203 (+244.07%)
Mutual labels:  events
Php Sse
A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than Websocket, instead of AJAX request.
Stars: ✭ 237 (+301.69%)
Mutual labels:  events
Event Driven Spring Boot
Example Application to demo various flavours of handling domain events in Spring Boot
Stars: ✭ 194 (+228.81%)
Mutual labels:  events
eloquence
Eloquence provides a cache on top of Eloquent that prevents multiple models being created for a single database row using the Identity Map design pattern.
Stars: ✭ 18 (-69.49%)
Mutual labels:  eloquent
Open Source Meetup Alternatives
Open-Source Alternatives to Meetup
Stars: ✭ 191 (+223.73%)
Mutual labels:  events
Mulog
μ/log is a micro-logging library that logs events and data, not words!
Stars: ✭ 222 (+276.27%)
Mutual labels:  events
Cqrs Clean Eventual Consistency
CQRS, using Clean Architecture, multiple databases and Eventual Consistency
Stars: ✭ 247 (+318.64%)
Mutual labels:  events

Laravel Attribute Observer

Laravel Attribute Observer - Social Image

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

Donate Buy Me A Coffee

Requirements

  • PHP: 7.4+
  • Laravel: 7+

Installation

You can install the package via composer:

composer require alexstewartja/laravel-attribute-observer

Configuration

Publish the config file (config/attribute-observer.php) with:

php artisan vendor:publish --provider="AlexStewartJA\LaravelAttributeObserver\LaravelAttributeObserverServiceProvider"

This is the default content of the published config file:

return [
    /*
    |--------------------------------------------------------------------------
    | Attribute Observers
    |--------------------------------------------------------------------------
    |
    | Here you may configure all desired Models and their respective Attribute
    | Observers. For example:
    |
    | 'observers' => [
    |    \App\Models\Order::class => [
    |        \App\AttributeObservers\OrderStatusObserver::class,
    |    ],
    | ]
    |
    */

    'observers' => [
        // Define your model & attribute observers here...
    ]
];

Populate the observers array with your desired Model => Attribute Observer mappings.

Usage

Attribute Observers

The make:laravel-attribute-observer Artisan command is the easiest way to create a new attribute observer class:

php artisan make:laravel-attribute-observer OrderStatusObserver --model=Order

This command will place the new attribute observer in your App/AttributeObservers directory. If this directory does not exist, Artisan will create it for you. Your freshly generated attribute observer will look like the following:

<?php

namespace App\AttributeObservers;

use App\Models\Order;

class OrderStatusObserver
{
    /**
     * Handle changes to the "id" field of Order on "created" events.
     *
     * @param \App\Models\Order $order
     * @param mixed $newValue The current value of the field
     * @param mixed $oldValue The previous value of the field
     * @return void
     */
    public function onIdCreated(Order $order, mixed $newValue, mixed $oldValue)
    {
        //
    }
}

You may then modify the code to match your business logic, for example:

<?php

namespace App\AttributeObservers;

use App\Events\OrderStatusChanged;
use App\Models\Order;

class OrderStatusObserver
{
    /**
     * Handle changes to the "status" field of Order on "saved" events.
     *
     * @param \App\Models\Order $order
     * @param mixed $newValue The current value of the field
     * @param mixed $oldValue The previous value of the field
     * @return void
     */
    public function onStatusSaved(Order $order, mixed $newValue, mixed $oldValue)
    {
        // Dispatch an event that sends an order update email to the customer
        OrderStatusChanged::dispatch($order);
    }
}

Attribute Observer methods are always supplied with the model instance, the new attribute value and the previous attribute value, in that order.

Events

You may observe all the typical CRUD events dispatched by Eloquent models during their lifecycles. Supported events are: creating, created, updating, updated, saving, saved, deleting, deleted.

The naming convention to follow when defining attribute observer methods is: on|AttributeName|Event

So, let's say we want to check a user's email against a global spam/vanity mail list each time they attempt to update it, we would implement that logic in an attribute observer method called onEmailUpdating.

Note that the attribute name must be in PascalCase and the event name must be Capitalized.

Testing

composer test

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

License

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

Donations

I maintain this package in my spare time. If it's beneficial to you, consider donating or buying me a coffee to keep it improving.

Donate Buy Me A Coffee

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].