Create a scope class and register it on the model — every query will include the constraint automatically.
// app/Models/Scopes/PublishedScope.php
namespace App\Models\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class PublishedScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$builder->where('status', 0);
}
}
// app/Models/Article.php
use App\Models\Scopes\PublishedScope;
class Article extends Model
{
protected static function booted(): void
{
static::addGlobalScope(new PublishedScope());
}
}
// Usage — scope applied automatically
$articles = Article::all(); // WHERE status = 0
$all = Article::withoutGlobalScopes()->get(); // bypasses scope
All Comments