Publisher fires numerous events throughout the publishing lifecycle. These allow you to hook into state transitions, validate changes, and perform side effects.
Fires before a model is published. Return false to cancel.
static::publishing(function (Post $post) {
if (!$post->isReadyForPublish()) {
return false; // Cancel publishing
}
});Fires after a model has been published successfully.
static::published(function (Post $post) {
Cache::forget("post.{$post->id}");
event(new PostPublished($post));
});Fires before a model is unpublished. Return false to cancel.
static::unpublishing(function (Post $post) {
if ($post->is_featured) {
return false; // Prevent unpublishing featured posts
}
});Fires after a model has been unpublished successfully.
static::unpublished(function (Post $post) {
Cache::forget("post.{$post->id}");
});Fires before attributes are saved to the draft column.
static::drafting(function (Post $post) {
// Modify attributes before drafting
$post->draft_updated_at = now();
});Fires after attributes have been saved to the draft column.
static::drafted(function (Post $post) {
Log::info("Post {$post->id} draft saved");
});Fires before draft attributes are moved to regular columns (during publish).
static::undrafting(function (Post $post) {
// Last chance to modify before publishing
});Fires after draft attributes have been moved to regular columns.
static::undrafted(function (Post $post) {
// Draft has been cleared
});Fires before a model is reverted to its published state.
static::reverting(function (Post $post) {
// About to discard draft changes
});Fires after a model has been reverted.
static::reverted(function (Post $post) {
Log::info("Post {$post->id} reverted to published state");
});Fires before a model is marked for deletion.
static::suspending(function (Section $section) {
// About to be queued for deletion
});Fires after a model has been marked for deletion.
static::suspended(function (Section $section) {
Log::info("Section {$section->id} suspended");
});Fires before a suspension is cleared.
static::resuming(function (Section $section) {
// About to be restored from suspension
});Fires after a suspension has been cleared.
static::resumed(function (Section $section) {
Log::info("Section {$section->id} resumed");
});These events fire during publishable relationship operations.
static::pivotDraftSyncing(function ($post, $relation, $ids, $attributes) {
// Before sync in draft mode
// $ids: array of IDs being synced
// $attributes: array of ID => attributes
});
static::pivotDraftSynced(function ($post, $relation, $changes, $attributes) {
// After sync in draft mode
// $changes contains:
// [
// 'draftAttached' => [1, 2], // IDs of newly attached pivots
// 'draftUpdated' => [3], // IDs of updated pivots
// 'draftDetached' => [4, 5], // IDs of detached pivots
// 'reattached' => [6], // IDs of pivots restored after a draft detach
// ]
// $attributes: array of ID => attributes for the attached, updated and reattached pivots
});static::pivotDraftAttaching(function ($post, $relation, $ids, $attributes) {
// Before attach in draft mode
});
static::pivotDraftAttached(function ($post, $relation, $ids, $attributes) {
// After attach in draft mode
});Fired when pivots are marked for deletion in draft mode. This event fires for all pivots regardless of their has_been_published status:
static::pivotDraftDetaching(function ($post, $relation, $ids) {
// Before pivots are marked for deletion (should_delete = true)
// $ids: array of related model IDs being detached
});
static::pivotDraftDetached(function ($post, $relation, $ids) {
// After pivots have been marked for deletion
});Note: This event fires consistently whether the pivot has been published or not. The actual deletion happens later during
flush()(on publish), which fires eitherpivotDiscarding/pivotDiscardedfor draft-only pivots orpivotDetaching/pivotDetachedfor published pivots.
static::pivotDraftUpdating(function ($post, $relation, $id, $attributes) {
// Before pivot attributes updated in draft
});
static::pivotDraftUpdated(function ($post, $relation, $id, $attributes) {
// After pivot attributes updated in draft
});Fired when pivots marked for deletion are restored:
static::pivotReattaching(function ($post, $relation, $ids, $attributes) {
// Before pivots marked for deletion are restored
// $ids: array of related model IDs being reattached
// $attributes: array of ID => attributes being applied
});
static::pivotReattached(function ($post, $relation, $ids, $attributes) {
// After pivots have been restored
});Fired when draft-only pivots (has_been_published = false) are permanently deleted. This typically happens during:
flush()when the parent is published (for pivots withshould_delete = true)discard()when reverting the parentrevert()on the parent model
static::pivotDiscarding(function ($post, $relation, $ids) {
// Before draft-only pivots are permanently deleted
// $ids: array of related model IDs being discarded
});
static::pivotDiscarded(function ($post, $relation, $ids) {
// After draft-only pivots have been deleted
});Note: When detaching pivots in draft mode,
pivotDraftDetaching/pivotDraftDetachedfires instead (markingshould_delete = true). ThepivotDiscarding/pivotDiscardedevents only fire during the actual permanent deletion.
Fired when pivots are published (during parent publish). This marks draft pivots as has_been_published = true:
static::pivotAttaching(function ($post, $relation, $ids) {
// Before draft pivots become published
});
static::pivotAttached(function ($post, $relation, $ids) {
// After draft pivots have been published
});Fired when published pivots (has_been_published = true) are permanently deleted during flush():
static::pivotDetaching(function ($post, $relation, $ids) {
// Before published pivots are permanently deleted
});
static::pivotDetached(function ($post, $relation, $ids) {
// After published pivots have been deleted
});When a parent model is published, the following pivot events fire in order:
pivotDiscarding/pivotDiscarded- Draft-only pivots (has_been_published = false) withshould_delete = trueare permanently deletedpivotDetaching/pivotDetached- Published pivots (has_been_published = true) withshould_delete = trueare permanently deletedpivotAttaching/pivotAttached- Draft pivots (has_been_published = false) withoutshould_deletebecome published
When a parent model is reverted:
pivotReattaching/pivotReattached- Pivots withshould_delete = trueare restoredpivotDiscarding/pivotDiscarded- Draft-only pivots (has_been_published = false) are permanently deleted
Pivot events only fire when the operation actually changes state. Repeated operations that don't change pivot state are no-ops and fire no events:
| Operation | No-Op Condition |
|---|---|
detach() in draft |
Pivot already has should_delete = true |
attach() in draft |
Pivot exists with should_delete = false |
reattach() |
Pivot doesn't have should_delete = true |
// First detach - fires pivotDraftDetaching/pivotDraftDetached
$post->tags()->detach($tagId);
// Second detach on same pivot - no events (already marked)
$post->tags()->detach($tagId);Register event listeners in your model's booted method:
class Post extends Model implements Publishable
{
use IsPublishable;
protected static function booted()
{
static::publishing(function (Post $post) {
// Validation
});
static::published(function (Post $post) {
// Side effects
});
}
}Or use an observer:
class PostObserver
{
public function publishing(Post $post): ?bool
{
if (!$post->isComplete()) {
return false;
}
return null;
}
public function published(Post $post): void
{
// Notify subscribers
}
}Register the observer:
// In AppServiceProvider
Post::observe(PostObserver::class);publishing → (save) → published
↓ ↓
undrafting → (save) → undrafted
unpublishing → (save) → unpublished
↓ ↓
drafting → (save) → drafted
reverting → (refresh/save) → reverted
Events that fire before an operation can return false to cancel:
static::publishing(function (Post $post) {
return false; // Prevents publishing
});
static::unpublishing(function (Post $post) {
return false; // Prevents unpublishing
});The model's update() or save() will return false if cancelled.
Use the withoutHandlers method to temporarily disable event handlers:
static::withoutHandlers(['publishing', 'published'], function () {
$post->update(['status' => Status::PUBLISHED]);
});static::publishing(function (Post $post) {
if (empty($post->title) || empty($post->content)) {
return false;
}
});static::published(function (Post $post) {
Cache::tags(['posts'])->flush();
});
static::unpublished(function (Post $post) {
Cache::tags(['posts'])->flush();
});static::published(function (Post $post) {
Notification::send(
$post->subscribers,
new PostPublishedNotification($post)
);
});static::publishing(function (Post $post) {
AuditLog::create([
'action' => 'publishing',
'model_type' => Post::class,
'model_id' => $post->id,
'user_id' => auth()->id(),
]);
});- Publishing Workflow - State transitions
- Dependent Models - Suspension events
- Publishable Relationships - Pivot events