chelout / laravel-relationship-events
Missing relationship events for Laravel
Installs: 1 392 362
Dependents: 21
Suggesters: 0
Security: 0
Stars: 522
Watchers: 14
Forks: 34
Open Issues: 8
Requires
- php: ^8.2
- illuminate/container: ^11.0
- illuminate/database: ^11.0
- illuminate/events: ^11.0
- illuminate/support: ^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.14
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.5|^11.0
- dev-master
- v3.0.0
- v2.0.0
- v1.5.0
- 1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6
- v0.5.1
- v0.5
- v0.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3
- 0.2
- 0.1
- dev-laravel-11
- dev-shift-112732
- dev-php8-1
- dev-develop
- dev-laravel-7
- dev-feature/laravel-6
- dev-feature/travisci
- dev-feature/tests-coverage
- dev-feature/tests
- dev-feature/pass-relation-to-dispatchable-event
- dev-feature/observables
- dev-feature/doc
This package is auto-updated.
Last update: 2024-11-15 11:55:33 UTC
README
Missing relationship events for Laravel
Install
- Install package with composer
Stable branch:
composer require chelout/laravel-relationship-events
Development branch:
composer require chelout/laravel-relationship-events:dev-master
- 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'); } }
- 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
- One To One Relations
- One To Many Relations
- Many To Many Relations
- Has Many Through Relations
- One To One Polymorphic Relations
- One To Many Polymorphic Relations
- Many To Many Polymorphic Relations
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:
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
:
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:
// ... $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