Laravel0Laravel 8.81 Released | Laravel News

[ad_1]

The Laravel team released 8.81 with a string scan method, the ability to disable caching in Attribute accessors, a getOrPut() collection method, and more:

Stringable Scan Method

Amir Rami contributed a scan method to stringable implemented using PHP’s sscanf function:

1$this->assertSame(

2 [123456],

3 $this->stringable('SN/123456')

4 ->scan('SN/%d')

5 ->toArray()

6);

7 

8$this->assertSame(

9 ['Otwell', 'Taylor'],

10 $this->stringable('Otwell, Taylor')

11 ->scan('%[^,],%s')

12 ->toArray()

13);

14 

15$this->assertSame(

16 ['filename', 'jpg'],

17 $this->stringable('filename.jpg')

18 ->scan('%[^.].%s')

19 ->toArray()

20);

Disable Caching in Attribute Accessors

Declan Norton contributed to the ability to disable caching for attributes being accessed via the new Attribute class. You can use the new API in a few ways:

1public function finish(): Attribute

2{

3 return Attribute::getWithoutCaching(

4 fn () => $this->start->addSeconds($this->duration)

5 );

6}

7 

8// or

9 

10public function finish(): Attribute

11{

12 return Attribute::get(

13 fn () => $this->start->addSeconds($this->duration)

14 )->disableObjectCaching();

15}

Here’s an explanation of the use-case from the pull request’s description:

I often find myself adding virtual accessors to construct objects based on other attributes within the model, most commonly DateTime instances.

Currently, the result of an accessor is cached if it’s an object and only invalidated if the attribute’s mutator is called. Scalar types are unaffected.

Get or Put Collection Method

@eusonlito contributed a getOrPut method to collections which simplifies the use-case where you either want to get an existing key or put the value if it doesn’t exist and return the value:

1// Still valid,

2if ($this->collection->has($key) === false) {

3 $this->collection->put($key, $this->create($data));

4}

5 

6return $this->collection->get($key);

7 

8// Using the `getOrPut()` method with closure

9return $this->collection->getOrPut($key, fn () => $this->create($data));

10 

11// Or pass a fixed value

12return $this->collection->getOrPut($key, $value);

Release Notes

You can see the complete list of new features and updates below and the diff between 8.80.0 and 8.81.0 on GitHub. The following release notes are directly from the changelog:

v8.81.0

Added

  • Added Illuminate/Support/Stringable::scan() (#40472)
  • Allow caching to be disabled for virtual attributes accessors that return an object (#40519)
  • Added better bitwise operators support (#40529, def671d)
  • Added getOrPut on Collection (#40535)
  • Improve PhpRedis flushing (#40544)
  • Added Illuminate/Support/Str::flushCache() (#40620)

Fixed

  • Fixed Str::headline/Str::studly with unicode and add Str::ucsplit method (#40499)
  • Fixed forgetMailers with MailFake (#40495)
  • Pruning Models: Get the default path for the models from a method instead (#40539)
  • Fix flushdb for predis cluste (#40446)
  • Avoid undefined array key 0 error (#40571)

Changed

  • Allow whitespace in PDO dbname for PostgreSQL (#40483)
  • Allows authorizeResource method to receive arrays of models and parameters (#40516)
  • Inverse morphable type and id filter statements to prevent SQL errors (#40523)
  • Bump voku/portable-ascii to v1.6.1 (#40588, #40610)

[ad_2]

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *