korridor/laravel-model-validation-rules
Installation Add the package via Composer:
composer require korridor/laravel-model-validation-rules
Publish the config (if needed):
php artisan vendor:publish --provider="Korridor\LaravelModelValidationRules\ServiceProvider"
Basic Usage Validate if a model exists by its ID or attribute:
use Korridor\LaravelModelValidationRules\Rules\Exists;
$validator = Validator::make($request->all(), [
'user_id' => ['required', new Exists(User::class)],
]);
First Use Case
Validate a belongsTo relationship in a form submission:
$validator = Validator::make($request->all(), [
'author_id' => ['required', new Exists(Author::class)],
]);
Validation in Form Requests
Extend FormRequest and use the rule:
public function rules()
{
return [
'category_id' => ['required', new Exists(Category::class)],
];
}
Dynamic Model Validation Pass a dynamic model class (e.g., from a config or API):
$modelClass = config('app.active_model');
$validator->addRules([
'reference_id' => [new Exists($modelClass)],
]);
Customizing Validation Logic Override default behavior (e.g., check for soft-deleted models):
$rule = new Exists(User::class, true); // Allow soft-deleted records
API Resource Validation Validate nested resources in API payloads:
$validator = Validator::make($request->all(), [
'post.category_id' => [new Exists(Category::class)],
]);
Integration with Laravel Policies Combine with policies for authorization:
$rule = new Exists(User::class, false, function ($identifier, $model) {
return $model->can('edit', auth()->user());
});
Performance with Large Datasets
exists in a query scope:
$validator->after(function ($validator) {
if ($validator->errors()->has('user_id')) {
$validator->errors()->add('user_id', 'Invalid user ID.');
}
});
Soft Deletes by Default
true to allow them:
new Exists(User::class, true) // Allows soft-deleted records
Case Sensitivity in Attributes
user_id vs UserId).Custom Primary Keys
id as the primary key. Specify a custom key:
new Exists(User::class, false, null, 'uuid')
Log Failed Validations Add a custom message for debugging:
new Exists(User::class, false, null, null, 'User with ID {0} does not exist.')
Check Model Events
If validation fails unexpectedly, verify retrieved or retrieving model events aren’t interfering.
Custom Validation Logic Pass a closure to validate additional conditions:
new Exists(User::class, false, function ($id, $user) {
return $user->isActive();
})
Extend the Rule Create a custom rule class:
class ActiveExists implements Rule {
public function passes($attribute, $value) {
return (new Exists(User::class, false, function ($id, $user) {
return $user->isActive();
}))->passes($attribute, $value);
}
}
Laravel 10+ Compatibility Ensure your Laravel version supports the package’s validation syntax. Test with:
use Illuminate\Validation\Rule as ValidationRule;
// Fallback to native rules if needed
Testing Mock the rule in tests:
$this->partialMock(Exists::class, 'passes')
->shouldReturn(false);
How can I help you explore Laravel packages today?