File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -392,6 +392,16 @@ public function resetJoinsWith(): void
392392 $ this ->joinsWith = [];
393393 }
394394
395+ public function resetWith (): static
396+ {
397+ $ this ->with = [];
398+ $ this ->joinsWith = array_map (
399+ static fn (JoinWith $ joinWith ) => $ joinWith ->withoutEagerLoading (),
400+ $ this ->joinsWith ,
401+ );
402+ return $ this ;
403+ }
404+
395405 public function innerJoinWith (array |string $ with , array |bool $ eagerLoading = true ): static
396406 {
397407 return $ this ->joinWith ($ with , $ eagerLoading , 'INNER JOIN ' );
Original file line number Diff line number Diff line change @@ -101,6 +101,16 @@ public function with(array|string ...$with): static;
101101 */
102102 public function getWith (): array ;
103103
104+ /**
105+ * Resets the relations that this query should be performed with.
106+ *
107+ * This method clears all relations set via {@see with()} and disables eager loading for relations
108+ * set via {@see joinWith()}, while keeping the JOIN clauses intact.
109+ *
110+ * @return static The query object itself.
111+ */
112+ public function resetWith (): static ;
113+
104114 /**
105115 * Specifies the relation associated with the junction table for use in a relational query.
106116 *
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ final class JoinWith
1818 */
1919 public function __construct (
2020 public readonly array $ relations ,
21- private readonly array |bool $ eagerLoading ,
21+ private array |bool $ eagerLoading ,
2222 private readonly array |string $ joinType ,
2323 ) {
2424 }
@@ -64,4 +64,16 @@ public function getJoinType(string $name): string
6464 ? ($ this ->joinType [$ name ] ?? 'INNER JOIN ' )
6565 : $ this ->joinType ;
6666 }
67+
68+ /**
69+ * Creates a new instance without eager loading.
70+ *
71+ * @return self A new instance without eager loading.
72+ */
73+ public function withoutEagerLoading (): self
74+ {
75+ $ new = clone $ this ;
76+ $ new ->eagerLoading = false ;
77+ return $ new ;
78+ }
6779}
Original file line number Diff line number Diff line change @@ -131,6 +131,27 @@ public function testGetWith(): void
131131 $ this ->assertSame (['orders ' , 'profile ' ], $ query ->getWith ());
132132 }
133133
134+ public function testResetWith (): void
135+ {
136+ $ query = Customer::query ()->with ('orders ' , 'profile ' );
137+
138+ $ query ->resetWith ();
139+
140+ $ this ->assertSame ([], $ query ->getWith ());
141+ }
142+
143+ public function testResetWithForJoinWith (): void
144+ {
145+ $ query = Customer::query ()->with ('orders ' )->joinWith ('profile ' );
146+
147+ $ query ->resetWith ();
148+ $ this ->assertSame ([], $ query ->getWith ());
149+
150+ $ joinsWith = $ query ->getJoinsWith ();
151+ $ this ->assertCount (1 , $ joinsWith );
152+ $ this ->assertSame ([], $ joinsWith [0 ]->getWith ());
153+ }
154+
134155 public function testInnerJoinWith (): void
135156 {
136157 $ joinsWith = Customer::query ()->innerJoinWith ('profile ' )->getJoinsWith ();
You can’t perform that action at this time.
0 commit comments