Laravel & Lumen validation rules (
unique_with_scope
,exists_with_scope
) that honor Eloquent model global scopes by leveraging the ORM instead of raw queries.
Laravel's native unique
, exists
validation rules do not respect global scopes defined on your Eloquent models.
If you’ve added a GlobalScope
to filter records (e.g., where('is_active', 1)
), Laravel’s native validation will bypass it and still validate against unscoped data.
This package uses Eloquent models directly for validation, ensuring:
- Your model’s global scopes are always applied.
- Validations like
unique
,exists
are checked in the same context as your application logic.
unique_with_scope
: Validate uniqueness using model & scoped columns.exists_with_scope
: Validate existence with model and scoped filters.- Supports Laravel and Lumen.
- Reuses model scopes and logic — DRY & accurate.
composer require nurazimzahurin/laravel-rule-with-scope
$request->validate([
'email' => 'required|email|unique_with_scope:App\\Models\\User,email'
]);
<model>,<column>,<except_id>,<except_column>
The rule will:
- Instantiate the Eloquent model.
- Apply all global scopes.
- Add
where
conditions using the provided scope columns. - Validate using
->exists()
or->where(...)->count()
.
You have a global scope on User
model:
protected static function booted()
{
static::addGlobalScope('active', function (Builder $builder) {
$builder->where('is_active', 1);
});
}
Laravel’s native validation will ignore it:
// This will wrongly match soft-deleted or inactive users
'email' => 'unique:users,email'
With this package:
// Will respect the 'active' global scope on User model
'email' => 'unique_with_scope:App\\Models\\User,email'
No setup required. Package uses auto-discovery.
Register the provider in bootstrap/app.php
:
$app->register(\YourVendor\UniqueWithScope\UniqueWithScopeRuleServiceProvider::class);
$app->withFacades();
'email.unique_with_scope' => 'This email is already taken in your organization.',
MIT © Nur Azim Zahurin
Built to make Laravel validation respect your domain logic — just like the rest of your app does.