User
model might be associated with many Posts
namespace App\Models;
use Artificertech\RelationshipEvents\Concerns\HasRelationshipEvents;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasRelationshipEvents;
/**
* Get the posts associated with the user.
*/
public function posts()
{
return $this->hasMany(Post::class)->withEvents();
}
}
Now we can use methods to assosiate User
with Post
and also update associated model.
// ...
$user = factory(User::class)->create([
'name' => 'John Smith',
]);
// Create posts and assosiate it with user
// This will fire two events hasManyCreating, hasManyCreated
$user->posts()->create([
'phone' => '8-800-123-45-67',
'email' => 'user@example.com',
'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// ...
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::hasManyCreating('posts', function ($user, $post) {
Log::info("Creating post: {$post->name} for user {$user->name}.");
});
static::hasManyCreated('posts', function ($user, $post) {
Log::info("Post: {$post->name} for user: {$user->name} has been created.");
});
static::hasManySaving('posts', function ($user, $post) {
Log::info("Saving post: {$post->name} for user {$user->name}.");
});
static::hasManySaved('posts', function ($user, $post) {
Log::info("Post: {$post->name} for user: {$user->name} has been saved.");
});
}
// ...
Dispatch Events with the event dispatcher
// ...
protected $dispatchesEvents = [
'postsCreating' => UserPostsCreating::class,
'postsCreated' => UserPostsCreated::class,
'postsSaving' => UserPostsSaveing::class,
'postsSaving' => UserPostsSaving::class,
];
// ...
Or you may use an Observer. Be sure to define the observable events in your model class - See Detecting Observable Events
namespace App\Observer;
class UserObserver
{
/**
* Handle the User "postsCreating" event.
*
* @param \App\Models\User $user
* @param \App\Models\Posts $post
*
* @return void
*/
public function postsCreating(User $user, Posts $post)
{
Log::info("Creating post: {$post->name} for user {$user->name}.");
}
/**
* Handle the User "postsCreated" event.
*
* @param \App\Models\User $user
* @param \App\Models\Posts $post
*
* @return void
*/
public function postsCreated(User $user, Posts $post)
{
Log::info("Post: {$post->name} for user: {$user->name} has been created.");
}
/**
* Handle the User "postsSaving" event.
*
* @param \App\Models\User $user
* @param \App\Models\Posts $post
*
* @return void
*/
public function postsSaving(User $user, Posts $post)
{
Log::info("Saving post: {$post->name} for user {$user->name}.");
}
/**
* Handle the User "postsSaved" event.
*
* @param \App\Models\User $user
* @param \App\Models\Posts $post
*
* @return void
*/
public function postsSaved(User $user, Posts $post)
{
Log::info("Post: {$post->name} for user: {$user->name} has been saved.");
}
}
- fires {relationship}Creating, {relationship}Created
- events have $parent and $related models
- fires {relationship}Saving, {relationship}Saved
- events have $parent and $related models