korridor/laravel-model-validation-rules
FormRequest, API Resources, or manual validation in controllers).ModelExists validation rule that can be used alongside Laravel’s native validation rules (e.g., exists:table,column). This makes it non-intrusive and easy to adopt incrementally.exists:model,field,relationship), enabling validation against related models (e.g., exists:User,email,profile).exists in raw SQL).Validation Scope:
Performance:
exists in raw SQL) that could reduce overhead?Maintenance:
Testing:
exists checks in FormRequest::rules() with ModelExists.POST /posts/{post}/comments where post must exist).user_id corresponds to an active user).exists or caching.exists:table,column rules with ModelExists for Eloquent models.User::rules()) or FormRequest classes.// Before
'user_id' => 'exists:users,id,deleted_at,null',
// After
'user_id' => [new \Korridor\ModelValidationRules\Exists('User')],
'profile_id' => [new \Korridor\ModelValidationRules\Exists('User', 'profile_id', 'profile')],
composer.json constraints). Test with the target Laravel version.exists failures).exists.ModelExists failures (e.g., "The selected user does not exist.").ValidationException handling).exists:users,id cached for 5 minutes).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database downtime | Validation fails silently | Fallback to cached values or graceful degradation. |
| Race conditions (e.g., record deleted between validation and save) | Inconsistent data state | Use database transactions or optimistic locking. |
| Malformed input (e.g., invalid ID) | Validation errors or SQL errors | Sanitize input early (e.g., integer rule before exists). |
| Package bugs | Unexpected validation behavior | Test thoroughly; fork if critical. |
How can I help you explore Laravel packages today?