From dfce8d78c77d4c730212516cce6837b32c5ebf6f Mon Sep 17 00:00:00 2001 From: Bert Heyman Date: Wed, 7 May 2025 15:08:52 +0200 Subject: [PATCH 1/2] Update eloquent-factories.md Be specific about recycle only working for 1-1 and 1-many --- eloquent-factories.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/eloquent-factories.md b/eloquent-factories.md index afb6383f02..84b0da32be 100644 --- a/eloquent-factories.md +++ b/eloquent-factories.md @@ -1,4 +1,4 @@ -# Eloquent: Factories +one# Eloquent: Factories - [Introduction](#introduction) - [Defining Model Factories](#defining-model-factories) @@ -616,7 +616,8 @@ public function definition(): array ### Recycling an Existing Model for Relationships -If you have models that share a common relationship with another model, you may use the `recycle` method to ensure a single instance of the related model is recycled for all of the relationships created by the factory. +If you have models that have a one-to-one or one-to-many relationship, you may use the `recycle` method to ensure a single instance of the related model is recycled for all of the relationships created by the factory. +To reuse models in a many-to-many relationship, see [HasAttached()](#pivot-table-attributes). For example, imagine you have `Airline`, `Flight`, and `Ticket` models, where the ticket belongs to an airline and a flight, and the flight also belongs to an airline. When creating tickets, you will probably want the same airline for both the ticket and the flight, so you may pass an airline instance to the `recycle` method: From b952b0819bbda6281a40d0313955ce819736ff45 Mon Sep 17 00:00:00 2001 From: Bert Heyman Date: Wed, 7 May 2025 15:13:23 +0200 Subject: [PATCH 2/2] Rephrase recycle restrictions --- eloquent-factories.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eloquent-factories.md b/eloquent-factories.md index 84b0da32be..4649992e29 100644 --- a/eloquent-factories.md +++ b/eloquent-factories.md @@ -616,8 +616,8 @@ public function definition(): array ### Recycling an Existing Model for Relationships -If you have models that have a one-to-one or one-to-many relationship, you may use the `recycle` method to ensure a single instance of the related model is recycled for all of the relationships created by the factory. -To reuse models in a many-to-many relationship, see [HasAttached()](#pivot-table-attributes). +For factories that include relationships, you may use the `recycle` method to ensure a single instance of the related model is recycled for all of the relationships created by the factory. +For relationships outside of the factory (like a many-to-many), see [HasAttached()](#pivot-table-attributes). For example, imagine you have `Airline`, `Flight`, and `Ticket` models, where the ticket belongs to an airline and a flight, and the flight also belongs to an airline. When creating tickets, you will probably want the same airline for both the ticket and the flight, so you may pass an airline instance to the `recycle` method: