Efficient Relationship Seeding with Laravel Factory Recycling
Overview of Model Recycling
Seeding a database often leads to a messy explosion of unwanted records. When multiple models depend on the same parent entity, traditionally creates a fresh instance for every single relationship. This behavior bloats your database and makes testing unpredictable. The recycle method solves this by allowing you to inject existing model instances into the factory chain. It ensures that instead of generating new duplicates, the factory "recycles" a specific record across all related models.
Prerequisites
To implement this technique, you should be comfortable with and the framework. Specifically, you need a working knowledge of models and . Familiarity with commands for database migrations is also required.
Key Libraries & Tools
- Laravel Framework: The core ecosystem providing the factory and seeding functionality.
- Eloquent ORM: The database abstraction layer used to define relationships.
- Database Factories: 's native system for generating fake data for testing and local development.
Code Walkthrough
Consider a scenario where a Ticket belongs to an Airline and a Flight, but the Flight also belongs to that same Airline. Without recycling, generating one ticket would create two separate airline records. Here is how to fix that:
// Create the shared instance first
$airline = Airline::factory()->create();
// Recycle that instance into the Ticket factory
$ticket = Ticket::factory()
->recycle($airline)
->create();
By calling recycle($airline), the factory engine scans for any relationship requiring an Airline model. Instead of hitting its own internal factory definition, it injects the ID of your pre-created $airline. You can also pass a collection of models. If you provide a collection of ten airlines, will randomly pick one for each record created, which is perfect for generating realistic distributions of data.
Syntax Notes
The recycle method is a fluent interface available on the builder. It accepts a single model instance or a . Behind the scenes, maps the model type to the relationship requirements defined in your factory's definition() method.
Practical Examples
This pattern is vital when building complex SaaS applications. If you are seeding "Projects" and "Tasks" that must all belong to the same "User," recycling ensures every task links back to a single user account rather than spawning dozens of fake users. It maintains data integrity and keeps your test environment clean.
Tips & Gotchas
Avoid the trap of assuming factories are smart enough to guess your intentions. If you don't explicitly use recycle, defaults to creating new records to satisfy foreign key constraints. Always run php artisan migrate:fresh before seeding to ensure you are analyzing the factory output on a clean slate.
- 46%· software
- 15%· software
- 8%· software
- 8%· software
- 8%· software
- Other topics
- 15%

Laravel Gems - Recycle Models in Factories ♻️
WatchLaravel // 2:58
The official YouTube channel of Laravel, the clean stack for Artisans and agents. We will update you on what's new in the world of Laravel, from the framework to our products Cloud, Forge, and Nightwatch.