Optimizing Factory Data Creation with Laravel's recycle Method

A passionate full-stack developer from @ePlus.DEV
Search for a command to run...

A passionate full-stack developer from @ePlus.DEV
No comments yet. Be the first to comment.
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling.
Introduction This package enables multi-level approval workflows for Eloquent models in your Laravel application. If you have models that require review and approval from multiple approvers before execution, this package provides a flexible approval ...
Daily Tech Brief — 06/08/2026 Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary Claude Code 2.1.223 vá nhiều đường обх

Overview In a challenge lab you’re given a scenario and a set of tasks. Instead of following step-by-step instructions, you will use the skills learned from the labs in the course to figure out how to

🏕️ Arcade Base Camp August 2026 Welcome to Arcade Base Camp August 2026, where you’ll develop key Google Cloud skills and earn an exclusive credential that will open doors to the cloud for you. No pr

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary Google Cloud giới thiệu hai Database Operations Agents hỗ trợ onboard

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary SK hynix và Sandisk công bố đặc tả mở đầu tiên cho High Bandwidth Fla

Laravel's factory system introduces the recycle method, allowing efficient reuse of model instances across multiple factory calls. This feature is particularly valuable when creating complex data structures with shared relationships.
// Basic recycling
$category = Category::factory()->create();
$products = Product::factory()
->count(3)
->recycle($category)
->create();
Let's explore a practical example of an e-commerce testing environment:
<?php
namespace Tests;
use App\Models\Store;
use App\Models\Product;
use App\Models\Category;
use App\Models\Order;
use Tests\TestCase;
class StoreTest extends TestCase
{
public function test_can_generate_sales_report()
{
// Create base structure
$store = Store::factory()->create();
$categories = Category::factory()
->count(3)
->recycle($store)
->create();
// Create products in categories
$products = Product::factory()
->count(20)
->recycle($store)
->recycle($categories)
->create();
// Generate orders using same products
$orders = Order::factory()
->count(50)
->recycle($store)
->recycle($products)
->create()
->each(function ($order) use ($products) {
// Add 1-5 random products to each order
$orderProducts = $products->random(rand(1, 5));
$order->products()->attach(
$orderProducts->pluck('id')->mapWithKeys(function ($id) {
return [$id => ['quantity' => rand(1, 5)]];
})
);
});
// Test report generation
$report = $store->generateSalesReport();
$this->assertNotNull($report);
$this->assertEquals(50, $report->total_orders);
}
}
The recycle method significantly improves factory performance by reusing existing models instead of creating new instances for each relationship.