reedware/laravel-modern-factories
Bring Laravel 8-style class-based model factories to legacy Laravel (5.1–7.x) apps. Write modern PHPUnit-friendly factories today to reduce upgrade pain later, without facades or service providers. Supports PHP 5.5–8.4.
Illuminate\Database\Eloquent\Factories namespace (backported), ensuring backward compatibility without polluting legacy codebases. Ideal for monolithic applications with tight coupling to older Laravel versions.new/for syntax).Factories in custom namespaces, conflicts may arise. Mitigation: Prefix factories or namespace isolation via use statements.afterCreating) may require Laravel 6+ for full functionality. Risk: Partial feature parity in Laravel 5.x.state(), afterMaking) to leverage the package effectively?newFactory vs. new).has(), for()).User, Post) to test factory behavior.| Feature | Laravel 5.1–5.6 | Laravel 6.x–7.x | Notes |
|---|---|---|---|
new() syntax |
❌ (use newFactory) |
✅ | PHP 7.0+ required for new. |
for() syntax |
❌ (use forModel) |
✅ | PHP 7.0+ required for for. |
state() |
✅ | ✅ | Works across all versions. |
afterCreating() |
❌ | ✅ | Requires Laravel 6+. |
Relationships (has()) |
✅ | ✅ | Limited by legacy Eloquent constraints. |
composer require reedware/laravel-modern-factories.composer.json to require Laravel 6+ if targeting advanced features.has() may not work as expected in Laravel 5.x).has() calls).| Risk | Impact | Mitigation |
|---|---|---|
| Package abandonment | Broken factories in future Laravel versions | Fork the repo; submit upstream fixes. |
| Incompatible with Laravel 7.x+ | Missing features (e.g., afterCreating) |
Target Laravel 6.x for full feature set. |
| Test suite breakage | Factories expose hidden dependencies | Run parallel tests with old/new factories. |
| PHP 5.5 syntax limitations | new/for syntax errors |
Use newFactory/forModel or upgrade PHP. |
| Over-reliance on factories | Brittle tests, slow suite execution | Pair with fixture tools (e.g., Laravel Dusk). |
newFactory vs. new).afterCreating does in LaravelHow can I help you explore Laravel packages today?