Update dependencies
Composer: Laravel 13.43, Octane 2.25, Fortify 1.43 and Pest 5.3.7, with the packages they bring (70 in all). This fixes the advisories in Laravel and the Symfony components. npm: Vite 8.3.8 with Rolldown 1.2.19, Playwright 1.65.1 and chokidar 6. The asset build, CI and the dev container use Node.js 26. The dev image also has git for Composer. Both audits are clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Eloquent Best Practices
|
||||
# Eloquent best practices
|
||||
|
||||
## Define Precise Relationship Types
|
||||
## Define precise relationship types
|
||||
|
||||
Define the relationship that matches the database association, and declare its concrete return type.
|
||||
|
||||
@@ -16,7 +16,7 @@ public function author(): BelongsTo
|
||||
}
|
||||
```
|
||||
|
||||
## Use Local Scopes for Reusable Queries
|
||||
## Use local scopes for reusable queries
|
||||
|
||||
Extract reusable query constraints into local scopes to avoid duplication.
|
||||
|
||||
@@ -43,9 +43,9 @@ $active = User::active()->get();
|
||||
$articles = Article::whereHas('user', fn ($q) => $q->active())->get();
|
||||
```
|
||||
|
||||
## Apply Global Scopes Sparingly
|
||||
## Apply global scopes sparingly
|
||||
|
||||
Global scopes silently modify every query on the model, making debugging difficult. Prefer local scopes and reserve global scopes for truly universal constraints like soft deletes or multi-tenancy.
|
||||
A global scope silently modifies every query on the model, so queries become hard to debug. Prefer local scopes, and reserve global scopes for universal constraints such as soft deletes or multi-tenancy.
|
||||
|
||||
Global scope tradeoff:
|
||||
|
||||
@@ -74,9 +74,9 @@ Post::published()->paginate(); // Explicit
|
||||
Post::paginate(); // Admin sees all
|
||||
```
|
||||
|
||||
## Define Attribute Casts
|
||||
## Define attribute casts
|
||||
|
||||
Use the `casts()` method (or `$casts` property following project convention) for automatic type conversion.
|
||||
Use the `casts()` method for automatic type conversion. If the project convention is the `$casts` property, follow that convention.
|
||||
|
||||
```php
|
||||
protected function casts(): array
|
||||
@@ -89,7 +89,7 @@ protected function casts(): array
|
||||
}
|
||||
```
|
||||
|
||||
## Cast Date and Time Attributes
|
||||
## Cast date and time attributes
|
||||
|
||||
Cast a date or timestamp attribute when application code should treat it as a Carbon instance. Eloquent already casts the conventional `created_at` and `updated_at` timestamps.
|
||||
|
||||
@@ -115,9 +115,9 @@ protected function casts(): array
|
||||
{{ $order->ordered_at->format('m-d') }}
|
||||
```
|
||||
|
||||
## Use `whereBelongsTo()` for Relationship Queries
|
||||
## Use `whereBelongsTo()` for relationship queries
|
||||
|
||||
`whereBelongsTo()` expresses the relationship constraint without manually specifying its foreign key.
|
||||
`whereBelongsTo()` expresses the relationship constraint without naming its foreign key.
|
||||
|
||||
Foreign key constraint:
|
||||
|
||||
@@ -132,9 +132,9 @@ Post::whereBelongsTo($user)->get();
|
||||
Post::whereBelongsTo($user, 'author')->get();
|
||||
```
|
||||
|
||||
## Keep Application Queries Model-Aware
|
||||
## Keep application queries model-aware
|
||||
|
||||
Prefer Eloquent models and relationships for model-backed application queries. They preserve casts, scopes, and model table configuration. The query builder and raw SQL legitimately require table names, so use them when their lower-level behavior is intentional.
|
||||
Prefer Eloquent models and relationships for model-backed application queries. They preserve casts, scopes, and model table configuration. The query builder and raw SQL require table names. Use them when you intend their lower-level behavior.
|
||||
|
||||
Lower-level alternatives:
|
||||
|
||||
@@ -153,6 +153,6 @@ User::where('active', true)->get();
|
||||
Order::where('status', 'pending')->get();
|
||||
```
|
||||
|
||||
When a query builder operation should follow a model's configured table name, use `(new User)->getTable()`. For complex joins or raw SQL, explicit table names may be clearer; keep those references covered by tests when schema changes are possible.
|
||||
When a query builder operation should follow a model's configured table name, use `(new User)->getTable()`. For complex joins or raw SQL, explicit table names may be clearer. When the schema can change, cover those table references with tests.
|
||||
|
||||
In migrations, use explicit table names rather than application models. Migrations are historical snapshots, while models and their scopes can change after a migration is deployed.
|
||||
In migrations, use explicit table names rather than application models. Migrations are historical snapshots, but models and their scopes can change after you deploy a migration.
|
||||
|
||||
Reference in New Issue
Block a user