Set Data on a Fluent Instance in Laravel 11.36

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 La

Set Data on a Fluent Instance in Laravel 11.36

ARE YOU TIRED OF LOW SALES TODAY?

Connect to more customers on doacWeb

Post your business here..... from NGN1,000

WhatsApp: 09031633831

ARE YOU TIRED OF LOW SALES TODAY?

Connect to more customers on doacWeb

Post your business here..... from NGN1,000

WhatsApp: 09031633831

ARE YOU TIRED OF LOW SALES TODAY?

Connect to more customers on doacWeb

Post your business here..... from NGN1,000

WhatsApp: 09031633831

Set Data on a Fluent Instance in Laravel 11.36

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


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.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow