Streamlining Eloquent: JSON Collections, Mass Assignment, and Automatic Eager Loading
Overview of Laravel 12.8 Enhancements
continues to refine the developer experience by removing friction from common tasks. Version 12.8 introduces three pivotal updates: a streamlined way to turn JSON directly into collections, the ability to bypass mass-assignment protections for bulk relationship creation, and a highly anticipated automatic eager loading feature to combat N+1 query issues. These tools target efficiency, allowing you to write cleaner code without the overhead of manual decoding or redundant eager loading calls.
Direct Collection Creation from JSON
Historically, converting a JSON string into a required a two-step process: decoding the string into an array and then wrapping it. The new fromJson() method simplifies this into a single call. This is particularly useful when handling API responses or configuration stored in database text fields.
// Old Way
$data = json_decode($jsonString, true);
$collection = collect($data);
// New Laravel 12.8 Way
$collection = Collection::fromJson($jsonString);
Bypassing Mass Assignment with forceCreateMany
When importing data, you might encounter models where specific fields aren't in the $fillable array for security reasons. Previously, if you wanted to bulk-insert these via a relationship, you had to loop and call forceCreate() individually. introduces forceCreateMany() to handle this efficiently.
$post->comments()->forceCreateMany([
['content' => 'First comment', 'user_id' => 1],
['content' => 'Second comment', 'user_id' => 2],
]);
You can also use forceCreateManyQuietly() to prevent model events from firing during the creation process.
Solving N+1 with Automatic Eager Loading
The most significant update is the introduction of automatic relation loading. This feature detects when a relationship is being accessed in a loop and automatically eager loads it to prevent excessive database queries. You can enable this globally in your AppServiceProvider or on a specific collection.
// Global enablement
Model::automaticallyEagerLoadRelationships();
// Dynamic enablement on a specific result set
$users = User::all()->withRelationshipAutoloading();
Tips & Gotchas
- Conventions Matter: Automatic eager loading relies on standard naming conventions. If your relationships use custom foreign keys that deviate from the standard, the automation might fail.
- JSON Integrity: Ensure your JSON is valid before using
fromJson(), as it still relies on successful decoding under the hood. - Force Create Risks: Only use
forceCreateMany()when you trust the source data, as it ignores all mass-assignment protections.
- 40%· software
- 20%· software
- 20%· software
- 20%· software

fromJson(), Force Create Many & Automatic Eager Loading in Laravel 12.8
WatchLaravel // 8:54
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.