Giter Club home page Giter Club logo

attribute-events's Introduction

Laravel Attribute Events Laravel Attribute Events

Build Status Last Updated Latest Stable Version License

class Order extends Model
{
    protected $dispatchesEvents = [
        'status:shipped' => OrderShipped::class,
        'note:*' => OrderNoteChanged::class,
    ];
}

Eloquent models fire several handy events throughout their lifecycle, like created and deleted. However, there are usually many more business meaningful events that happen during a model's life. With this library you can capture those, by mapping attribute changes to your own event classes.

Installation

composer require jpkleemans/attribute-events

How to use it

Use the Kleemans\AttributeEvents trait in your model and add the attributes to the $dispatchesEvents property:

class Order extends Model
{
    use AttributeEvents;

    protected $dispatchesEvents = [
        'created'         => OrderPlaced::class,
        'status:canceled' => OrderCanceled::class,
        'note:*'          => OrderNoteChanged::class,
    ];
}

The attribute events will be dispatched after the updated model is saved. Each event receives the instance of the model through its constructor.

For more info on model events and the $dispatchesEvents property, visit the Laravel Docs

Listening

Now you can subscribe to the events via the EventServiceProvider $listen array, or manually with Closure based listeners:

Event::listen(function (OrderCanceled $event) {
    // Restock inventory
});

Or push realtime updates to your users, using Laravel's broadcasting feature:

Echo.channel('orders')
    .listen('OrderShipped', (event) => {
        // Display a notification
    })

JSON attributes

For attributes stored as JSON, you can use the -> operator:

protected $dispatchesEvents = [
    'payment->status:completed' => PaymentCompleted::class,
];

Accessors

For more complex state changes, you can use attributes defined by an accessor:

class Product extends Model
{
    protected $dispatchesEvents = [
        'low_stock:true' => ProductReachedLowStock::class,
    ];

    public function getLowStockAttribute(): bool
    {
        return $this->stock <= 3;
    }
}

You can also use the new way of defining accessors introduced in Laravel 9.

Learn more

Sponsors

Nexxtmove Logo

Thanks to Nexxtmove for sponsoring the development of this project.
Your logo or name here? Sponsor this project.

License

Code released under the MIT License.

attribute-events's People

Contributors

jpkleemans avatar dependabot[bot] avatar ariaieboy avatar ee-bhushan avatar bhushan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.