Backend Boilerplates php Beginner

Laravel Eloquent Published Scope

Reusable Eloquent local scope to filter only published records.

php
// In your model
public function scopePublished(Builder $query): Builder {
    return $query->where('is_published', true)
                ->whereNotNull('published_at')
                ->where('published_at', '<=', now());
}

// Usage
$modules = Module::published()->latest('published_at')->paginate(12);

Prerequisites

  • Laravel >= 10

Pro Tips

  • → Chain with other scopes freely.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
php
Level
Beginner
Read time
2 min
Published
Aug 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.