chelout/laravel-relationship-events
Adds missing Eloquent relationship events to Laravel models. Use simple traits (HasOne/Many, BelongsTo/Many, Morph*) to listen for attach/detach/sync, saved/updated, and other relation lifecycle hooks with parent/related context and IDs/attributes.
Post model have many Comment models
namespace App\Models;
use Chelout\RelationshipEvents\Concerns\HasMorphManyEvents;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasMorphManyEvents;
protected $fillable = [
'title',
'body',
];
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Now we can use methods to assosiate Post with Comment and also update assosiated models.
// ...
// create post
$post = factory('App\Models\Post')->create();
// create comment
$comment = factory('App\Models\Comment')->create();
// Saving comment with specified post
// This will fire two events morphManySaving, morphManySaved
$post->comments()->save($comment);
// ...
Now we should listen our events, for example we can register event listners in model's boot method:
// ...
public static function boot()
{
parent::boot();
/**
* One To Many Polymorphic Relationship Events
*/
static::morphManySaving(function ($parent, $related) {
Log::info("Saving comment's post.");
});
static::morphManySaved(function ($parent, $related) {
Log::info("Comment's post is now set.");
});
}
// ...
Note: has additional query to get related Eloquent collection
There is also inverse relation Comment belongs to Post
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Chelout\RelationshipEvents\Concerns\HasMorphToEvents;
class Comment extends Model
{
use HasMorphToEvents;
/**
* Get the post associated with the comment.
*/
public function post()
{
return $this->morphTo(Post::class);
}
}
Now we can use methods to assosiate, dissosiate and update Post with Comment:
// ...
// create post
$post = factory('App\Models\Post')->create();
// create comment
$comment = factory('App\Models\Comment')->create();
// Saving comment with specified post
// This will fire two events morphToAssociating, morphToAssociated
$comment->post()->associate($post);
// ...
Now we should listen our events, for example we can register event listners in model's boot method:
// ...
protected static function boot()
{
parent::boot();
static::morphToAssociating(function ($relation, $related, $parent) {
Log::info("Associating post {$parent->name} with comment.");
});
static::morphToAssociated(function ($relation, $related, $parent) {
Log::info("Comment has been assosiated with post {$parent->name}.");
});
}
// ...
Note: related model is dirty, should be saved after associating
Note: has additional query to get parent model Note: related model is dirty, should be saved after dissociating
Note: has additional query to get related model
How can I help you explore Laravel packages today?