You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .ai/patterns/database-patterns.md
+53Lines changed: 53 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -243,6 +243,59 @@ Server::chunk(100, function ($servers) {
243
243
-**Composite indexes** for common queries
244
244
-**Unique constraints** for business rules
245
245
246
+
### Request-Level Caching with ownedByCurrentTeamCached()
247
+
248
+
Many models have both `ownedByCurrentTeam()` (returns query builder) and `ownedByCurrentTeamCached()` (returns cached collection). **Always prefer the cached version** to avoid duplicate database queries within the same request.
-`ownedByCurrentTeamCached()` - **Default choice** for reading team data
275
+
-`ownedByCurrentTeam()` - Only when you need to chain query builder methods that can't be done on collections (like `with()` for eager loading), or when you explicitly need a fresh database query
276
+
277
+
**Implementation pattern for new models:**
278
+
```php
279
+
/**
280
+
* Get query builder for resources owned by current team.
281
+
* If you need all resources without further query chaining, use ownedByCurrentTeamCached() instead.
282
+
*/
283
+
public static function ownedByCurrentTeam()
284
+
{
285
+
return self::whereTeamId(currentTeam()->id);
286
+
}
287
+
288
+
/**
289
+
* Get all resources owned by current team (cached for request duration).
0 commit comments