Publisher provides query scopes and draft-aware WHERE clause handling for querying publishable models.
The PublisherScope is automatically applied to all publishable models:
Only published models are visible:
Publisher::restrictDraftContent();
Post::all(); // Only status = 'published'All models except suspended ones are visible:
Publisher::allowDraftContent();
Post::all(); // All except should_delete = trueuse Plank\Publisher\Scopes\PublisherScope;
// Get all models regardless of status
Post::withoutGlobalScope(PublisherScope::class)->get();Only return published models:
Post::onlyPublished()->get();
// SQL: WHERE status = 'published'Only return draft models:
Post::onlyDraft()->get();
// SQL: WHERE status != 'published'Include suspended models (normally hidden):
Post::withQueuedDeletes()->get();
// Includes models with should_delete = trueExplicitly exclude suspended models (default behavior, but can be useful for clarity):
Post::withoutQueuedDeletes()->get();
// Excludes models with should_delete = trueOnly return suspended models:
Post::onlyQueuedDeletes()->get();
// SQL: WHERE should_delete = trueWhen draft content is allowed, WHERE clauses on draftable columns automatically check both the published column value and the draft JSON:
Publisher::allowDraftContent();
Post::where('title', 'Hello')->get();This generates SQL like:
SELECT * FROM posts WHERE (
(status = 'published' AND title = 'Hello')
OR
(status != 'published' AND draft->>'title' = 'Hello')
)This ensures queries work correctly whether viewing the published value or draft value.
- All model attributes that are not excluded from drafting
- Does NOT apply to: primary key, timestamps, publisher columns, foreign keys
Draft-aware WHERE clauses only apply when:
Publisher::draftContentAllowed()istrue- The column is not excluded from drafting
- The query is using the
PublisherBuilder
// Published posts with a specific tag
Post::onlyPublished()
->whereHas('tags', fn($q) => $q->where('name', 'Laravel'))
->get();
// All drafts including suspended
Post::onlyDraft()
->withQueuedDeletes()
->get();When querying relationships on publishable models:
$post = Post::find(1);
// The relationship query respects draft content visibility
$post->sections()->get();
// If you need to bypass:
$post->sections()->withoutGlobalScope(PublisherScope::class)->get();Aggregate queries also respect the global scope:
Publisher::restrictDraftContent();
Post::count(); // Only counts published
Post::max('views'); // Only from published
Publisher::allowDraftContent();
Post::count(); // Counts all non-suspendedPagination works with the scopes:
Post::onlyPublished()->paginate(10);
Post::onlyDraft()->paginate(10);Eager loaded relationships also respect visibility:
// Loads only published posts with their published sections
Publisher::restrictDraftContent();
Post::with('sections')->get();
// Loads all non-suspended posts with their sections
Publisher::allowDraftContent();
Post::with('sections')->get();// Query with specific visibility
$published = Publisher::withoutDraftContent(function () {
return Post::where('featured', true)->get();
});
$all = Publisher::withDraftContent(function () {
return Post::where('featured', true)->get();
});If you need to bypass Publisher's query modifications entirely:
use Illuminate\Support\Facades\DB;
// Direct database query
$posts = DB::table('posts')
->where('status', 'published')
->get();You can query the draft column directly if needed:
// Find posts with specific draft value
Post::whereJsonContains('draft->tags', 'important')->get();
// Check if draft exists
Post::whereNotNull('draft')->get();- Draft-aware WHERE is more complex - Adds OR conditions, may affect index usage
- Use specific scopes when possible -
onlyPublished()is simpler than general queries - Index appropriately - Consider indexes on
statusandshould_deletecolumns - Cache published content - Published content is stable, cache it aggressively
The PublisherBuilder extends Laravel's query builder with:
| Method | Description |
|---|---|
onlyPublished() |
Filter to published only |
onlyDraft() |
Filter to drafts only |
withQueuedDeletes() |
Include suspended |
withoutQueuedDeletes() |
Exclude suspended |
onlyQueuedDeletes() |
Only suspended |
- Draft Management - How drafts work
- Middleware - Controlling draft visibility per request
- IsPublishable - Trait reference