Git Product home page Git Product logo

laravel-pivot-events's Introduction

Easily add Eloquent model pivot events to your Laravel app

Latest Version on Packagist Build Status StyleCI Quality Score Total Downloads

The signifly/laravel-pivot-events package allows you to easily add Eloquent model pivots events to your Laravel app.

Below is a small example of how to use it.

// Remember to add use statement
use Signifly\PivotEvents\HasPivotEvents;

class User
{
    use HasPivotEvents;
}

Now you would be able to listen for the newly available pivot events:

use Signifly\PivotEvents\HasPivotEvents;

class User
{
    use HasPivotEvents;

    protected static function boot()
    {
        static::pivotAttaching(function ($model) {
            // To get related changes
            $model->getPivotChanges();
            // return Collection(['attach' => ['roles' => [1 => ['scopes' => 'orders']]]])

            // To get related changes for a specific type
            $model->getPivotChanges('attach');
            // return Collection(['roles' => [1 => ['scopes' => 'orders']]])
            
            // You can get nested changes
            // values are $id => $attributes
            $model->getPivotChanges('attach.roles');
            // return Collection([1 => ['scopes' => 'orders']])

            // To get related ids for a specific type and relation
            $model->getPivotChangeIds('attach', 'roles');
            // return Collection([1])
        });

        static::pivotAttached(function ($model) {
            //
        });

        static::pivotDetaching(function ($model) {
            //
        });

        static::pivotDetached(function ($model) {
            //
        });

        static::pivotUpdating(function ($model) {
            //
        });

        static::pivotUpdated(function ($model) {
            //
        });
    }
}

You can also use observers for the available events: pivotAttaching, pivotAttached, pivotDetaching, pivotDetached, pivotUpdating, pivotUpdated.

Documentation

Until further documentation is provided, please have a look at the tests.

Installation

You can install the package via composer:

$ composer require signifly/laravel-pivot-events

The package will automatically register itself.

Testing

$ composer test

Security

If you discover any security issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

laravel-pivot-events's People

Contributors

matthewnessworthy avatar pactode avatar razorsheep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-pivot-events's Issues

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.