mvanduijker/laravel-model-exists-rule
FormRequest/Validator architecture. It complements Laravel’s built-in Rule::exists() by enabling Eloquent-specific queries (e.g., whereHas, joins, custom scopes).Rule::exists() for specific cases).whereHas with nested joins) could introduce N+1 or inefficient subqueries if not optimized. Risk mitigated by leveraging Eloquent’s query caching or with().globalScope) may alter behavior unpredictably.withTrashed()) if not already configured in the query.Rule::exists() calls, or only for complex queries?Rule::exists() that need updating?Validator::make()).spatie/laravel-validation-rules for broader features.User, Order) to validate integration and performance.Rule::exists() calls with ModelExists for complex cases.Rule::exists() to leverage ModelExists where needed.Model::findOrFail() in controllers) into rules.ModelExists rules.->remember()) or preload relationships where applicable.composer require mvanduijker/laravel-model-exists-rule
'user_id' => ['required', Rule::exists('users', 'id')]
'user_id' => ['required', new ModelExists(\App\Models\User::class, 'id')]
new ModelExists(\App\Models\Order::class, 'id')
->where('status', 'completed')
->whereHas('user', fn($q) => $q->active())
Rule::exists() may evolve; assess if migration is needed when major Laravel updates occur.->toSql() for debugging).Model::find() calls in controllers.->remember(60)).with() in the model).| Scenario | Impact | Mitigation |
|---|---|---|
| Invalid model class | Validation fails silently | Use class_exists() checks or try-catch. |
| Unoptimized queries | Slow validation | Profile and add query caching. |
| Soft-deleted models | False negatives | Explicitly include withTrashed(). |
| Eloquent global scopes | Unexpected query behavior | Test with all active scopes. |
| Package abandonment | No future updates | Fork or replace if critical. |
whereHas).Rule::exists() for simplicity. Highlight benefits (e.g., Eloquent features, DRY validation).How can I help you explore Laravel packages today?