2017 © Pedro Peláez
 

library ignorable-observers

Dynamically disable/enable Laravel's Eloquent model observers

image

zachflower/ignorable-observers

Dynamically disable/enable Laravel's Eloquent model observers

  • Tuesday, February 7, 2017
  • by zachflower
  • Repository
  • 2 Watchers
  • 10 Stars
  • 31,783 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 19 % Grown

The README.md

Ignorable Observers

Code Climate build Packagist, (*1)

Dynamically disable/enable Laravel's Eloquent model observers. This library provides the ability to temporarily disable observable events for Eloquent models. For example, temporarily disable observers that kick off emails, push notifications, or queued calculations when performing a large number of database inserts or updates., (*2)

Installation

Install using composer:, (*3)

composer require zachflower/ignorable-observers

Usage

To give an Eloquent model the ability to temporarily ignore observers, simply add the IgnorableObservers trait:, (*4)

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use IgnorableObservers\IgnorableObservers;

class ExampleModel extends Model {
  use IgnorableObservers;
}

Then, call the ignoreObservableEvents() static method to ignore all observers for that model:, (*5)

ExampleModel::ignoreObservableEvents();

The ignoreObservableEvents() method also accepts an array of observers to be ignored. For example, the following line would ignore only the saved and created observers:, (*6)

ExampleModel::ignoreObservableEvents(['saved', 'created']);

To stop ignoring a model's observers, call the unignoreObservableEvents() static method:, (*7)

ExampleModel::unignoreObservableEvents();

The unignoreObservableEvents() method also accepts an array of observers to unignore, giving you total control over which observers to enable and disable:, (*8)

ExampleModel::unignoreObservableEvents(['saved']);

Example

The following example ignores any saved and created observers for the ExampleModel, inserts 100 rows into the database, and then "unignores" those observers when the operation is completed:, (*9)

ExampleModel::ignoreObservableEvents('saved', 'created');

for ( $i = 0; $i <= 100; $i++ ) {
  ExampleModel::create([
    'data' => $i
  ]);
}

ExampleModel::unignoreObservableEvents();

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Ignorable Observers is an open-sourced library licensed under the MIT license., (*10)

The Versions

07/02 2017

dev-master

9999999-dev

Dynamically disable/enable Laravel's Eloquent model observers

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zachary Flower

laravel library eloquent

07/02 2017

v0.1.0

0.1.0.0

Dynamically disable/enable Laravel's Eloquent model observers

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zachary Flower