├── .styleci.yml ├── CHANGELOG.md ├── LICENSE.md ├── composer.json ├── src ├── BelongsToMany.php └── HasPivotEvents.php └── README.md /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-pivot-events` will be documented in this file 4 | 5 | ## 1.5.1 - 2020-09-14 6 | 7 | - Add support for Laravel 8 8 | 9 | ## 1.5.0 - 2020-03-03 10 | 11 | - Add support for Laravel 7 12 | 13 | ## 1.4.0 - 2019-09-09 14 | 15 | - Add support for Laravel 6.0 16 | 17 | ## 1.3.0 - 2019-03-06 18 | 19 | - Add support for Laravel 5.8 20 | 21 | ## 1.2.0 - 2018-09-27 22 | 23 | - Add support for Laravel 5.7 24 | 25 | ## 1.1.2 - 2018-09-05 26 | 27 | - Fix: Parse id when attaching or updating existing pivot data 28 | 29 | ## 1.1.1 - 2018-05-16 30 | 31 | - Add support for laravel 5.5 32 | - Remove composer.lock 33 | 34 | ## 1.1.0 - 2018-04-25 35 | 36 | - Return collections from pivot changes and allow to get only ids 37 | 38 | ## 1.0.1 - 2018-04-24 39 | 40 | - Update observable events 41 | 42 | ## 1.0.0 - 2018-04-24 43 | 44 | - Initial release! 45 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Signifly 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "signifly/laravel-pivot-events", 3 | "description": "Trigger Eloquent model pivot events when attaching, detaching or updating.", 4 | "homepage": "https://github.com/signifly/laravel-pivot-events", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Morten Poul Jensen", 9 | "email": "mpj@signifly.com", 10 | "role": "Developer" 11 | } 12 | ], 13 | "require": { 14 | "php": "^7.4|^8.0", 15 | "illuminate/database": "^8.0|^9.0|^10.0" 16 | }, 17 | "require-dev": { 18 | "mockery/mockery": "^1.6.6", 19 | "orchestra/testbench": "^6.0|^7.0|^8.0", 20 | "phpunit/phpunit": "^8.0|^9.0|^10.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Signifly\\PivotEvents\\": "src" 25 | } 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Signifly\\PivotEvents\\Test\\": "tests" 30 | } 31 | }, 32 | "scripts": { 33 | "test": "vendor/bin/phpunit" 34 | }, 35 | "config": { 36 | "sort-packages": true 37 | }, 38 | "extra": { 39 | "laravel": { 40 | "providers": [ 41 | ], 42 | "aliases": { 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/BelongsToMany.php: -------------------------------------------------------------------------------- 1 | parent->setPivotChanges('attach', $this->getRelationName(), [ 20 | $this->parseId($id) => $attributes, 21 | ]); 22 | 23 | if ($this->parent->firePivotAttachingEvent() === false) { 24 | return false; 25 | } 26 | 27 | $result = parent::attach($id, $attributes, $touch); 28 | 29 | $this->parent->firePivotAttachedEvent(); 30 | 31 | $this->parent->resetPivotChanges(); 32 | 33 | return $result; 34 | } 35 | 36 | /** 37 | * Detach models from the relationship. 38 | * 39 | * @param mixed $ids 40 | * @param bool $touch 41 | * @return int 42 | */ 43 | public function detach($ids = null, $touch = true) 44 | { 45 | if (is_null($ids)) { 46 | $ids = $this->query->pluck( 47 | $this->query->qualifyColumn($this->relatedKey) 48 | )->toArray(); 49 | } 50 | 51 | $idsWithAttributes = collect($ids)->mapWithKeys(function ($id) { 52 | return [$id => []]; 53 | })->all(); 54 | 55 | $this->parent->setPivotChanges('detach', $this->getRelationName(), $idsWithAttributes); 56 | 57 | if ($this->parent->firePivotDetachingEvent() === false) { 58 | return false; 59 | } 60 | 61 | $result = parent::detach($ids, $touch); 62 | 63 | $this->parent->firePivotDetachedEvent(); 64 | 65 | $this->parent->resetPivotChanges(); 66 | 67 | return $result; 68 | } 69 | 70 | /** 71 | * Update an existing pivot record on the table. 72 | * 73 | * @param mixed $id 74 | * @param array $attributes 75 | * @param bool $touch 76 | * @return int 77 | */ 78 | public function updateExistingPivot($id, array $attributes, $touch = true) 79 | { 80 | $this->parent->setPivotChanges('update', $this->getRelationName(), [ 81 | $this->parseId($id) => $attributes, 82 | ]); 83 | 84 | if ($this->parent->firePivotUpdatingEvent() === false) { 85 | return false; 86 | } 87 | 88 | $result = parent::updateExistingPivot($id, $attributes, $touch); 89 | 90 | $this->parent->firePivotUpdatedEvent(); 91 | 92 | $this->parent->resetPivotChanges(); 93 | 94 | return $result; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easily add Eloquent model pivot events to your Laravel app 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/signifly/laravel-pivot-events.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-pivot-events) 4 | [![Build Status](https://img.shields.io/travis/signifly/laravel-pivot-events/master.svg?style=flat-square)](https://travis-ci.org/signifly/laravel-pivot-events) 5 | [![StyleCI](https://styleci.io/repos/130852365/shield?branch=master)](https://styleci.io/repos/130852365) 6 | [![Quality Score](https://img.shields.io/scrutinizer/g/signifly/laravel-pivot-events.svg?style=flat-square)](https://scrutinizer-ci.com/g/signifly/laravel-pivot-events) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/signifly/laravel-pivot-events.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-pivot-events) 8 | 9 | The `signifly/laravel-pivot-events` package allows you to easily add Eloquent model pivots events to your Laravel app. 10 | 11 | Below is a small example of how to use it. 12 | 13 | ```php 14 | // Remember to add use statement 15 | use Signifly\PivotEvents\HasPivotEvents; 16 | 17 | class User 18 | { 19 | use HasPivotEvents; 20 | } 21 | ``` 22 | 23 | Now you would be able to listen for the newly available pivot events: 24 | 25 | ```php 26 | use Signifly\PivotEvents\HasPivotEvents; 27 | 28 | class User 29 | { 30 | use HasPivotEvents; 31 | 32 | protected static function boot() 33 | { 34 | static::pivotAttaching(function ($model) { 35 | // To get related changes 36 | $model->getPivotChanges(); 37 | // return Collection(['attach' => ['roles' => [1 => ['scopes' => 'orders']]]]) 38 | 39 | // To get related changes for a specific type 40 | $model->getPivotChanges('attach'); 41 | // return Collection(['roles' => [1 => ['scopes' => 'orders']]]) 42 | 43 | // You can get nested changes 44 | // values are $id => $attributes 45 | $model->getPivotChanges('attach.roles'); 46 | // return Collection([1 => ['scopes' => 'orders']]) 47 | 48 | // To get related ids for a specific type and relation 49 | $model->getPivotChangeIds('attach', 'roles'); 50 | // return Collection([1]) 51 | }); 52 | 53 | static::pivotAttached(function ($model) { 54 | // 55 | }); 56 | 57 | static::pivotDetaching(function ($model) { 58 | // 59 | }); 60 | 61 | static::pivotDetached(function ($model) { 62 | // 63 | }); 64 | 65 | static::pivotUpdating(function ($model) { 66 | // 67 | }); 68 | 69 | static::pivotUpdated(function ($model) { 70 | // 71 | }); 72 | } 73 | } 74 | ``` 75 | 76 | You can also use observers for the available events: `pivotAttaching`, `pivotAttached`, `pivotDetaching`, `pivotDetached`, `pivotUpdating`, `pivotUpdated`. 77 | 78 | ## Documentation 79 | Until further documentation is provided, please have a look at the tests. 80 | 81 | ## Installation 82 | 83 | You can install the package via composer: 84 | 85 | ```bash 86 | $ composer require signifly/laravel-pivot-events 87 | ``` 88 | 89 | The package will automatically register itself. 90 | 91 | ## Testing 92 | ```bash 93 | $ composer test 94 | ``` 95 | 96 | ## Security 97 | 98 | If you discover any security issues, please email dev@signifly.com instead of using the issue tracker. 99 | 100 | ## Credits 101 | 102 | - [Morten Poul Jensen](https://github.com/pactode) 103 | - [Travis Elkins](https://github.com/telkins) 104 | - [All contributors](../../contributors) 105 | 106 | ## License 107 | 108 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 109 | -------------------------------------------------------------------------------- /src/HasPivotEvents.php: -------------------------------------------------------------------------------- 1 | each(function ($attributes, $id) use ($type, $relation) { 16 | data_set($this->pivotChanges, "{$type}.{$relation}.{$id}", $attributes); 17 | }); 18 | } 19 | 20 | public function getPivotChanges($type = null): Collection 21 | { 22 | return $type 23 | ? collect(data_get($this->pivotChanges, $type)) 24 | : collect($this->pivotChanges); 25 | } 26 | 27 | public function getPivotChangeIds($type, $relation): Collection 28 | { 29 | return collect($this->getPivotChanges("{$type}.{$relation}"))->keys(); 30 | } 31 | 32 | public function resetPivotChanges(): void 33 | { 34 | $this->pivotChanges = []; 35 | } 36 | 37 | public static function pivotAttaching($callback) 38 | { 39 | static::registerModelEvent('pivotAttaching', $callback); 40 | } 41 | 42 | public static function pivotAttached($callback) 43 | { 44 | static::registerModelEvent('pivotAttached', $callback); 45 | } 46 | 47 | public static function pivotDetaching($callback) 48 | { 49 | static::registerModelEvent('pivotDetaching', $callback); 50 | } 51 | 52 | public static function pivotDetached($callback) 53 | { 54 | static::registerModelEvent('pivotDetached', $callback); 55 | } 56 | 57 | public static function pivotUpdating($callback) 58 | { 59 | static::registerModelEvent('pivotUpdating', $callback); 60 | } 61 | 62 | public static function pivotUpdated($callback) 63 | { 64 | static::registerModelEvent('pivotUpdated', $callback); 65 | } 66 | 67 | public function firePivotAttachingEvent($halt = true) 68 | { 69 | return $this->fireModelEvent('pivotAttaching', $halt); 70 | } 71 | 72 | public function firePivotAttachedEvent($halt = false) 73 | { 74 | return $this->fireModelEvent('pivotAttached', $halt); 75 | } 76 | 77 | public function firePivotDetachingEvent($halt = true) 78 | { 79 | return $this->fireModelEvent('pivotDetaching', $halt); 80 | } 81 | 82 | public function firePivotDetachedEvent($halt = false) 83 | { 84 | return $this->fireModelEvent('pivotDetached', $halt); 85 | } 86 | 87 | public function firePivotUpdatingEvent($halt = true) 88 | { 89 | return $this->fireModelEvent('pivotUpdating', $halt); 90 | } 91 | 92 | public function firePivotUpdatedEvent($halt = false) 93 | { 94 | return $this->fireModelEvent('pivotUpdated', $halt); 95 | } 96 | 97 | /** 98 | * Get the observable event names. 99 | * 100 | * @return array 101 | */ 102 | public function getObservableEvents() 103 | { 104 | return array_merge( 105 | parent::getObservableEvents(), 106 | [ 107 | 'pivotAttaching', 'pivotAttached', 108 | 'pivotDetaching', 'pivotDetached', 109 | 'pivotUpdating', 'pivotUpdated', 110 | ] 111 | ); 112 | } 113 | 114 | /** 115 | * Instantiate a new BelongsToMany relationship. 116 | * 117 | * @param \Illuminate\Database\Eloquent\Builder $query 118 | * @param \Illuminate\Database\Eloquent\Model $parent 119 | * @param string $table 120 | * @param string $foreignPivotKey 121 | * @param string $relatedPivotKey 122 | * @param string $parentKey 123 | * @param string $relatedKey 124 | * @param string $relationName 125 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany 126 | */ 127 | protected function newBelongsToMany( 128 | Builder $query, 129 | Model $parent, 130 | $table, 131 | $foreignPivotKey, 132 | $relatedPivotKey, 133 | $parentKey, 134 | $relatedKey, 135 | $relationName = null 136 | ) { 137 | return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); 138 | } 139 | } 140 | --------------------------------------------------------------------------------