This week, the Laravel team released v11.36, which includes a chainable Fluent::set() method, a default global alias for the new Uri class, and more.
Fluent set() Method
Steve Bauman contributed a Fluent::set() method, which adds a chainable method that supports dot notation to set deeply nested attributes:
$fluent = new Fluent;
// Set basic attributes
$fluent->set('product', 'iPhone')
->set('version', 15)
->set('developer', 'Apple');
// Use dot notation for nested attributes
$fluent->set('specs.color', 'Space Black')
->set('specs.storage', '256GB')
->set('specs.price.usd', 1199);
// Retrieve values
echo $fluent->product; // "iPhone"
echo $fluent->specs['color']; // "Space Black"
echo $fluent->specs['price']['usd']; // 1199
// Retrieve values using get with dot notation
echo $fluent->get('specs.color'); // "Space Black"
echo $fluent->get('specs.price.usd'); // 1199
The implementation uses Laravel's data_set() function, which is a useful helper to work with nested array data:
$data = ['products' => ['desk' => ['price' => 100]]];
data_set($data, 'products.desk.price', 200);
data_get($data, 'products.desk.price'); // 200
For further details on setting data on a fluent instance, see Pull Request #53946.
Uri and UriQueryString implement Stringable
Luke Kuzmish contributed by adding the Stringable interface to the Uri and UriQueryString classes. These classes already implemented the Stringable interface, which is simply the __toString() magic method:
use Illuminate\Support\Uri;
$uri = Uri::of('https://laravel.com')
->withQuery(['name' => 'Taylor'])
->withPath('/docs/installation')
->withFragment('hello-world');
// https://laravel.com/docs/installation?name=Taylor#hello-world
(string) $uri;
See our post on Laravel v11.35's URI Parsing and Mutation features to learn more.
Uri Added as a Default, Global Alias
Jason McCreary added the new Uri class as a default global alias. That means you can access the Uri class directly without the Illuminate\Support namespace:
$uri = Uri::of('https://laravel.com')->withQuery(['name' => 'Taylor']);
See Pull Request #53884 for details.
Release notes
You can see the complete list of new features and updates below and the diff between 11.35.0 and 11.36.0 on GitHub. The following release notes are directly from the changelog:
v11.36.0
- [11.x] Update
config/mail.phpwith supported configuration by @crynobone in https://github.com/laravel/framework/pull/53874 - [11.x] Allows
enum_value()to be use in standaloneilluminate/collectionsby @crynobone in https://github.com/laravel/framework/pull/53871 - [11.x]
UriandUriQueryStringimplementStringableby @cosmastech in https://github.com/laravel/framework/pull/53873 - [11.x] Prefer
new StringableoverStr::ofandstr()by @lucasmichot in https://github.com/laravel/framework/pull/53883 - [11.x] No need to redeclare variables by @lucasmichot in https://github.com/laravel/framework/pull/53887
- [11.x] Add PHP 8.4 with herd to passthrough variables by @lucasmichot in https://github.com/laravel/framework/pull/53885
- Add new
Uriclass to default, global aliases by @jasonmccreary in https://github.com/laravel/framework/pull/53884 - [11.x] Fix attribute mutator access in
loadMissingby @SychO9 in https://github.com/laravel/framework/pull/53879 - [11.x] Fix
numericAggregateon eloquent builder by @AmirRezaM75 in https://github.com/laravel/framework/pull/53880 - [11.x] Prefer
new Fluentoverfluent()helper by @lucasmichot in https://github.com/laravel/framework/pull/53890 - Patch by @angelej in https://github.com/laravel/framework/pull/53869
- [11.x]
Collection::wrapby @lucasmichot in https://github.com/laravel/framework/pull/53891 - [11.x] Bump minimum league/commonmark by @ah-rahimi in https://github.com/laravel/framework/pull/53899
- [11.x]
Collection::rangeby @lucasmichot in https://github.com/laravel/framework/pull/53895 - [11.x] Added an event that reports files being deleted when calling the
schema:dump --prunecommand by @andrey-helldar in https://github.com/laravel/framework/pull/53870 - [11.x] fix: allows injection using multiple interfaces with the same concrete implementation by @jamiethorpe in https://github.com/laravel/framework/pull/53275
- [11.x] Early return in Factory::modelName() by @shaedrich in https://github.com/laravel/framework/pull/53912
- [11.x] Prevent
blankHelper from Serializing Eloquent Models by @SanderMuller in https://github.com/laravel/framework/pull/53911 - [11.x] Add word-break to mail links by @seblavoie in https://github.com/laravel/framework/pull/53906
- Preserve dynamic database connections on reconnect by @nickakitch in https://github.com/laravel/framework/pull/53914
- Fix mutexName inconsistency caused by different PHP binary paths on multiple servers by @waska14 in https://github.com/laravel/framework/pull/53811
- [11.x] Add
Fluent::setmethod by @stevebauman in https://github.com/laravel/framework/pull/53946 - [11.x] Fix inspecting columns of raw indexes by @hafezdivandari in https://github.com/laravel/framework/pull/53945
- [11.x] Allow easier overriding of the exception thrown by invalid ID in route binding by @cosmastech in https://github.com/laravel/framework/pull/53944
- [11.x] Fix client path value in file uploads by @gyaaniguy in https://github.com/laravel/framework/pull/53941
The post Set Data on a Fluent Instance in Laravel 11.36 appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
