Mastering New Eloquent Features in Laravel v11.39 & v11.40
Overview
Laravel continues to refine the developer experience by reducing boilerplate and enhancing database safety. Recent updates to (v11.39 and v11.40) introduce smarter ways to handle atomic database operations and model relationships. These features, specifically incrementOrCreate and withAttributes, solve common friction points where developers previously had to write manual checks or redundant code to ensure data consistency.
Prerequisites
To follow this tutorial, you should have a baseline understanding of and the . Familiarity with and basic Model relationships (like hasMany) will help you implement these patterns in your own applications.
Key Libraries & Tools
- : The primary framework providing these new features.
- : Laravel's active record implementation used for database interactions.
- : The command-line interface where destructive command protections are enforced.
Atomic Updates with IncrementOrCreate
Tracking metrics, such as page views or inventory, often requires checking if a record exists before updating it. The new incrementOrCreate method simplifies this logic into a single, clean call.
// The old way
$view = PageView::where('path', '/blog')->first();
if ($view) {
$view->increment('count');
} else {
PageView::create(['path' => '/blog', 'count' => 1]);
}
// The Laravel 11.40 way
PageView::incrementOrCreate(['path' => '/blog'], 'count');
This method looks for a record matching the first array. If found, it increments the specified column. If not, it creates the record with the matching attributes and initializes the counter. It eliminates the "if/else" clutter and ensures your database state stays predictable.
Persistent Logic with WithAttributes
A common pitfall in occurs when creating models through filtered relationships. If you have a relationship defined with a where clause, creating a new model through that relationship doesn't automatically apply those constraints to the new record.
// Relationship definition using withAttributes
public function hiddenPodcasts(): HasMany
{
return $this->hasMany(Podcast::class)
->withAttributes(['hidden' => true]);
}
By using withAttributes, the hidden => true constraint applies to both queries (fetching existing hidden podcasts) and creations. When you call $user->hiddenPodcasts()->create(), the new podcast automatically receives the hidden = true attribute without manual intervention.
Syntax Notes
- Attributes: The
#[UseFactory]attribute now allows explicit factory class definitions directly on the model, bypassing traditional naming conventions. - Method Chaining: Both
incrementOrCreateandwithAttributesfollow 's fluent interface, allowing them to feel natural within existing query chains.
Tips & Gotchas
Always remember that prohibitDestructiveCommands now includes migrate:rollback. While this protects production data, ensure your deployment scripts account for this restriction to avoid failed CI/CD pipelines. For withAttributes, note that it also works seamlessly within , making it a powerful tool for maintaining data integrity across different parts of your application.
- 29%路 software
- 21%路 software
- 14%路 languages
- 7%路 software
- 7%路 people
- Other topics
- 21%

Increment Or Create & Model Attributes in Laravel v11.39 & v11.40
WatchLaravel // 7:30
The official YouTube channel of Laravel, the clean stack for Artisans and agents. We will update you on what's new in the world of Laravel, from the framework to our products Cloud, Forge, and Nightwatch.