Publisher extends Laravel's belongsToMany and morphToMany relationships to support draft states for pivot table records.
Standard Laravel relationships don't consider publishing states. When you attach or detach related models, the changes are immediate. Publishable relationships allow these changes to be drafted and only applied when the parent model is published.
use Plank\Publisher\Relations\PublishableBelongsToMany;
class Post extends Model implements Publishable
{
use IsPublishable;
protected array $publishablePivottedRelations = ['tags'];
public function tags(): PublishableBelongsToMany
{
return $this->publishableBelongsToMany(Tag::class);
}
}use Plank\Publisher\Relations\PublishableMorphToMany;
class Post extends Model implements Publishable
{
use IsPublishable;
protected array $publishablePivottedRelations = ['media'];
public function media(): PublishableMorphToMany
{
return $this->publishableMorphToMany(Media::class, 'mediable');
}
}Publishable pivot tables need additional columns:
Schema::create('post_tag', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->foreignId('tag_id')->constrained()->cascadeOnDelete();
// Required for publishable pivots
$table->boolean('has_been_published')->default(false);
$table->boolean('should_delete')->default(false);
$table->json('draft')->nullable();
$table->timestamps();
});You must register publishable relationships in the $publishablePivottedRelations property:
class Post extends Model implements Publishable
{
use IsPublishable;
protected array $publishablePivottedRelations = [
'tags',
'categories',
'media',
];
}This tells Publisher to manage these relationships during publish/revert operations.
Attaching and detaching are immediate:
$post->status = Status::PUBLISHED;
$post->save();
// Attachment is immediate
$post->tags()->attach($tag);
// Pivot: has_been_published = true, should_delete = false
// Detachment is immediate
$post->tags()->detach($tag);
// Pivot record deleted immediatelyChanges are queued for the next publish:
$post->status = Status::DRAFT;
$post->save();
// New attachment is drafted
$post->tags()->attach($newTag);
// Pivot: has_been_published = false, should_delete = false
// Fires: pivotDraftAttaching, pivotDraftAttached
// Detaching marks pivot for deletion (consistent for all pivot types)
$post->tags()->detach($tag);
// Fires: pivotDraftDetaching, pivotDraftDetached
// Pivot: should_delete = true (deleted on publish)Note: Detaching in draft mode behaves consistently regardless of whether the pivot has been published. All pivots are marked with
should_delete = trueand the actual deletion happens during publish. The events fired during publish differ based onhas_been_publishedstatus (see "On Publish" below).
When you attach an ID that has a pivot marked for deletion (should_delete = true), the system automatically reattaches it instead of creating a new pivot:
// Tag was previously detached (should_delete = true)
$post->tags()->attach($previouslyDetachedTag, ['position' => 5]);
// Fires: pivotReattaching, pivotReattached (not pivotDraftAttaching)
// Pivot: should_delete = false, position stored in draftWhen the parent is published, pivot changes are finalized in this order:
-
Discard draft-only marked pivots - Pivots with
should_delete = trueANDhas_been_published = falseare permanently deleted- Fires:
pivotDiscarding,pivotDiscarded
- Fires:
-
Detach published marked pivots - Pivots with
should_delete = trueANDhas_been_published = trueare permanently deleted- Fires:
pivotDetaching,pivotDetached
- Fires:
-
Publish draft pivots - Pivots with
has_been_published = false(withoutshould_delete) are marked as published- Fires:
pivotAttaching,pivotAttached
- Fires:
-
Publish pivot attributes - Draft attribute values are moved to their regular columns
// Publishing the post finalizes all pivot changes
$post->update(['status' => Status::PUBLISHED]);When the parent is reverted, pivot changes are rolled back in this order:
-
Restore marked pivots - Pivots with
should_delete = truehave the flag cleared- Fires:
pivotReattaching,pivotReattached
- Fires:
-
Discard draft-only pivots - Pivots with
has_been_published = falseare permanently deleted- Fires:
pivotDiscarding,pivotDiscarded
- Fires:
-
Clear pivot draft attributes - Draft attribute values are reset to null
// Reverting discards pending pivot changes
$post->revert();All standard relationship methods work with draft awareness:
// Attach
$post->tags()->attach($tagId);
$post->tags()->attach([$tag1, $tag2]);
$post->tags()->attach($tagId, ['extra' => 'data']);
// Detach
$post->tags()->detach($tagId);
$post->tags()->detach([$tag1, $tag2]);
$post->tags()->detach(); // Detach all
// Sync
$post->tags()->sync([$tag1, $tag2]);
$post->tags()->sync([$tag1 => ['extra' => 'data']]);
// Toggle
$post->tags()->toggle([$tag1, $tag2]);// Publish pending pivot changes
$post->tags()->publish();
// Discard unpublished attachments
$post->tags()->discard();
// Restore pivots marked for deletion (should_delete = true)
$post->tags()->reattach();
$post->tags()->reattach([$tag1, $tag2]); // Specific IDs only
// Force delete pivots marked for deletion (should_delete = true)
$post->tags()->forceDetach();
$post->tags()->forceDetach([$tag1, $tag2]); // Specific IDs only
// Delete all queued-for-deletion records
$post->tags()->flush();
// Publish pivot attribute drafts
$post->tags()->publishPivotAttributes();
// Revert pivot attribute drafts
$post->tags()->revertPivotAttributes();Publishable relationships fire additional events:
| Event | When |
|---|---|
pivotDraftSyncing |
Before sync operation in draft |
pivotDraftSynced |
After sync operation in draft |
pivotDraftAttaching |
Before attach creates new pivot in draft |
pivotDraftAttached |
After attach creates new pivot in draft |
pivotDraftDetaching |
Before detach marks pivot (should_delete = true) |
pivotDraftDetached |
After detach marks pivot |
pivotDraftUpdating |
Before pivot attributes update in draft |
pivotDraftUpdated |
After pivot attributes update in draft |
pivotReattaching |
Before restoring marked pivot (should_delete = false) |
pivotReattached |
After restoring marked pivot |
| Event | When |
|---|---|
pivotAttaching |
Before draft pivot becomes published |
pivotAttached |
After draft pivot becomes published |
pivotDetaching |
Before published pivot is permanently deleted |
pivotDetached |
After published pivot is permanently deleted |
pivotDiscarding |
Before draft-only pivot is permanently deleted |
pivotDiscarded |
After draft-only pivot is permanently deleted |
class Post extends Model implements Publishable
{
use IsPublishable;
protected static function booted()
{
static::pivotDraftAttached(function ($post, $relation, $ids) {
// Log when tags are attached in draft mode
});
}
}The global scope handles pivot visibility:
// With draft content restricted: only published attachments visible
Publisher::restrictDraftContent();
$post->tags; // Only tags with has_been_published = true
// With draft content allowed: all non-deleted attachments visible
Publisher::allowDraftContent();
$post->tags; // All tags except should_delete = trueFor pivot attribute drafting, use a custom pivot model:
use Illuminate\Database\Eloquent\Relations\Pivot;
use Plank\Publisher\Concerns\HasPublishablePivotAttributes;
class PostTag extends Pivot
{
use HasPublishablePivotAttributes;
protected $table = 'post_tag';
}
class Post extends Model implements Publishable
{
use IsPublishable;
public function tags(): PublishableBelongsToMany
{
return $this->publishableBelongsToMany(Tag::class)
->using(PostTag::class)
->withPivot(['position', 'featured', 'draft']);
}
}See Custom Pivot Models for details.
For models related to themselves:
class Post extends Model implements Publishable
{
use IsPublishable;
protected array $publishablePivottedRelations = ['relatedPosts'];
public function relatedPosts(): PublishableBelongsToMany
{
return $this->publishableBelongsToMany(Post::class, 'post_post', 'post_id', 'related_post_id');
}
}- Always register relations - Add to
$publishablePivottedRelations - Add required columns - Include
has_been_publishedandshould_deleteon pivot tables - Consider draft column - Add
draftif you need pivot attribute drafting - Test both states - Verify behavior when parent is published vs draft
- Handle events - Use pivot events for side effects
- HasPublishablePivotAttributes - Pivot attribute drafting
- Custom Pivot Models - Advanced configurations
- Events - All lifecycle events