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.");
});
}
// ...
- fires hasManyCreating, hasManyCreated
- events have $parent and $related models
- fires hasManyCreating, hasManyCreated
- events have $parent and $related models
- fires hasManyUpdating, hasManyUpdated
- events have $parent model and $related Eloquent collection
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}.");
});
}
// ...
- fires belongToAssociating, belongToAssociated
- events have $relation name, $related and $parent models.
Note: related model is dirty, should be saved after associating
- fires belongToAssociating, belongToAssociated
- events have $relation name, $related and $parent models.
Note: has additional query to get parent model Note: related model is dirty, should be saved after dissociating
- fires belongToUpdating, belongToUpdated
- events have $relation name, $related and $parent models.
Note: has additional query to get related model