2017 © Pedro Peláez
 

library laravel-relationship-events

Missing relationship events for Laravel

image

chelout/laravel-relationship-events

Missing relationship events for Laravel

  • Friday, July 27, 2018
  • by chelout
  • Repository
  • 4 Watchers
  • 134 Stars
  • 486 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 12 Versions
  • 181 % Grown

The README.md

Laravel Relationship Events

Missing relationship events for Laravel, (*1)

Build Status Total Downloads Latest Stable Version License , (*2)

Install

  1. Install package with composer

Stable branch:

composer require chelout/laravel-relationship-events

Development branch:

composer require chelout/laravel-relationship-events:dev-master
  1. Use necessary trait in your model.

Available traits:

  • HasOneEvents
  • HasBelongsToEvents
  • HasManyEvents
  • HasBelongsToManyEvents
  • HasMorphOneEvents
  • HasMorphToEvents
  • HasMorphManyEvents
  • HasMorphToManyEvents
  • HasMorphedByManyEvents

use Chelout\RelationshipEvents\Concerns\HasOneEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasOneEvents; public static function boot() { parent::boot(); /** * One To One Relationship Events */ static::hasOneSaved(function ($parent, $related) { dump('hasOneSaved', $parent, $related); }); static::hasOneUpdated(function ($parent, $related) { dump('hasOneUpdated', $parent, $related); }); } }

use Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasMorphToManyEvents; public static function boot() { parent::boot(); /** * Many To Many Polymorphic Relations Events. */ static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) { dump('morphToManyAttached', $relation, $parent, $ids, $attributes); }); static::morphToManyDetached(function ($relation, $parent, $ids) { dump('morphToManyDetached', $relation, $parent, $ids); }); } public function tags() { return $this->morphToMany(Tag::class, 'taggable'); } }
  1. Dispatchable relationship events. It is possible to fire event classes via $dispatchesEvents properties and adding HasDispatchableEvents trait:

use Chelout\RelationshipEvents\Concerns\HasOneEvents; use Chelout\RelationshipEvents\Traits\HasDispatchableEvents; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasDispatchableEvents; use HasOneEvents; protected $dispatchesEvents = [ 'hasOneSaved' => HasOneSaved::class, ]; }

Relationships

Observers

Starting from v0.4 it is possible to use relationship events in Laravel observers classes Usage is very simple. Let's take User and Profile classes from One To One Relations, add HasRelationshipObservables trait to User class. Define observer class:, (*3)

namespace App\Observer;

class UserObserver
{
    /**
     * Handle the User "hasOneCreating" event.
     *
     * @param \App\Models\User $user
     * @param \Illuminate\Database\Eloquent\Model $related
     *
     * @return void
     */
    public function hasOneCreating(User $user, Model $related)
    {
        Log::info("Creating profile for user {$related->name}.");
    }

    /**
     * Handle the User "hasOneCreated" event.
     *
     * @param \App\Models\User $user
     * @param \Illuminate\Database\Eloquent\Model $related
     *
     * @return void
     */
    public function hasOneCreated(User $user, Model $related)
    {
        Log::info("Profile for user {$related->name} has been created.");
    }
}

Don't forget to register an observer in the boot method of your AppServiceProvider:, (*4)

namespace App\Providers;

use App\Models\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
// ...
    public function boot()
    {
        User::observe(UserObserver::class);
    }
// ...
}

And now just create profile for user:, (*5)

// ...
$user = factory(User::class)->create([
    'name' => 'John Smith',
]);

// Create profile and assosiate it with user
// This will fire two events hasOneCreating, hasOneCreated
$user->profile()->create([
    'phone' => '8-800-123-45-67',
    'email' => 'user@example.com',
    'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// ...

Todo

  • Tests, tests, tests

The Versions

27/07 2018

dev-master

9999999-dev https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

27/07 2018

dev-develop

dev-develop https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

27/07 2018

v0.4

0.4.0.0 https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

27/07 2018

dev-feature/observables

dev-feature/observables https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

24/07 2018

v0.3.3

0.3.3.0 https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

24/07 2018

dev-feature/doc

dev-feature/doc https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

01/05 2018

v0.3.2

0.3.2.0 https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

01/05 2018

v0.3.1

0.3.1.0 https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

12/04 2018

v0.3

0.3.0.0 https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

12/04 2018

dev-feature/refactoring

dev-feature/refactoring https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

28/03 2018

0.2

0.2.0.0 https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations

28/03 2018

0.1

0.1.0.0 https://github.com/chelout/laravel-relationship-events

Missing relationship events for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel events relationship relations