The Laravel team released v12.8.0, which includes automatic eager relation loading, creating a Collection instance from a JSON string, and more:
Automatic Relation Loading
Serhii Litvinchuk contributed automatic relationship autoloading:
In large projects, it can become difficult to track and manually specify which relations should be eager-loaded, especially if those relations are deeply nested or dynamically used. Therefore, automatic relation loading can be useful.
// instead of this $projects->load([ 'client.owner.details', 'client.customPropertyValues', 'clientContact.customPropertyValues', 'status', 'company.statuses', 'posts.authors.articles.likes', 'related.statuses']); // We can use this$projects->withRelationshipAutoloading();
Here's another example from the pull request's description:
$orders = Order::all()->withRelationshipAutoloading(); foreach ($orders as $order) { echo $order->client->owner->company->name;} // automatic calls:// $orders->loadMissing('client');// $orders->loadMissing('client.owner');// $orders->loadMissing('client.owner.company');
You can enable automatic loading across models using the following static method:
Model::automaticallyEagerLoadRelationships();
Create a Collection from JSON
@DarkGhostHunter contributed a fromJson
method to the Collection class, which you can use to create a collection from a JSON string:
// Before$collection = new Collection(json_decode($json, true)); // After$collection = Collection::fromJson($json);
The fromJson()
method also supports the depth and flags arguments you might pass to json_decode()
:
$collection = Collection::fromJson(json: $json, flags: JSON_THROW_ON_ERROR);
"Create Many" Variants of HasOneOrMany Relations
Philip Iezzi contributed forceCreateMany()
and forceCreateManyQuietly()
Eloquent methods for the HasOneOrMany
relationship:
$post->comments()->forceCreateMany($data);$post->comments()->forceCreateManyQuietly($data);
Here's an example from the Pull Request description, which would require force-creating individual models:
// Beforecollect($comments) ->map(fn (array $comment) => [ ...$comment, // override some attributes here ]) ->each(fn (array $comment) => $post->comments()->forceCreateQuietly($comment)); // Aftercollect($comments) ->map(fn (array $comment) => [ ...$comment, // override some attributes here ]) ->pipe($post->comments()->forceCreateManyQuietly(...));
Release notes
You can see the complete list of new features and updates below and the diff between 12.7.0 and 12.8.0 on GitHub. The following release notes are directly from the changelog:
v12.8.0
- [12.x] only check for soft deletes once when mass-pruning by @cosmastech in https://github.com/laravel/framework/pull/55274
- [12.x] Add createMany mass-assignment variants to
HasOneOrMany
relation by @onlime in https://github.com/laravel/framework/pull/55262 - cosmetic: include is_array() case in match construct of getArrayableItems by @epic-64 in https://github.com/laravel/framework/pull/55275
- Add tests for InvokeSerializedClosureCommand by @Amirhf1 in https://github.com/laravel/framework/pull/55281
- [12.x] Temporarily prevents PHPUnit 12.1 by @crynobone in https://github.com/laravel/framework/pull/55297
- [12.x] Test Improvements by @crynobone in https://github.com/laravel/framework/pull/55306
- Bump vite from 5.4.12 to 5.4.17 in /src/Illuminate/Foundation/resources/exceptions/renderer by @dependabot in https://github.com/laravel/framework/pull/55301
- [12.x] Test Improvements by @crynobone in https://github.com/laravel/framework/pull/55307
- [12.x] add generics to array types for Schema Grammars by @taka-oyama in https://github.com/laravel/framework/pull/55314
- [12.x] fix missing nullable for Query/Grammar::compileInsertGetId by @taka-oyama in https://github.com/laravel/framework/pull/55311
- [12.x] Adds
fromJson()
to Collection by @DarkGhostHunter in https://github.com/laravel/framework/pull/55310 - [12.x] Fix
illuminate/database
usage as standalone package by @crynobone in https://github.com/laravel/framework/pull/55309 - Correct array key in InteractsWithInput by @AJenbo in https://github.com/laravel/framework/pull/55287
- [12.x] Fix support for adding custom observable events from traits by @willrowe in https://github.com/laravel/framework/pull/55286
- [12.x] Added Automatic Relation Loading (Eager Loading) Feature by @litvinchuk in https://github.com/laravel/framework/pull/53655
- [12.x] Modify PHPDoc for Collection::chunkWhile functions to support preserving keys by @jsvdvis in https://github.com/laravel/framework/pull/55324
- [12.x] Introduce Rule::anyOf() for Validating Against Multiple Rule Sets by @brianferri in https://github.com/laravel/framework/pull/55191