Laravel0Laravel 9.18 Released | Laravel News

[ad_1]

The Laravel team released 9.18 jam-packed with amazing features and quality-of-life improvements. Let’s look at the high-level details of the standout features now available in Laravel 9!

Improve file attachments for mail and notifications

Tim MacDonald contributed attachable objects, which you can use to implement attachments with models:

1namespace App\Models;

2 

3use Illuminate\Contracts\Mail\Attachable;

4use Illuminate\Database\Eloquent\Model;

5use Illuminate\Mail\Attachment;

6 

7class Photo extends Model implements Attachable

8{

9 /**

10 * Get the attachable representation of the model.

11 *

12 * @return \Illuminate\Mail\Attachment

13 */

14 public function toMailAttachment()

15 {

16 return Attachment::fromPath('/path/to/file');

17 }

18}

When building an email message, you can pass an instance of the model via the attach() method:

1public function build()

2{

3 return $this->view('photos.resized')

4 ->attach($this->photo);

5}

See the new attachable objects documentation for complete details on this beneficial feature!

Invokable validation classes

Tim MacDonald contributed invokable validation classes:

This PR aims to introduce a new class based validation implementation that mixes the brevity + simplicity of Closure based rules with the shareable, extendable, and chainable nature of class based rules by introducing an Invokable validation rule.

Here’s an example implementation from Pull Request #42689:

1class InvokableQuantity implements InvokableRule

2{

3 public function __invoke($attribute, $value, $fail)

4 {

5 if (! is_array($value)) {

6 return $fail('validation.quantity.must_be_an_object')->translate();

7 }

8 

9 if (! array_key_exists('magnitude', $value)) {

10 $fail('validation.quantity.missing_magnitude')->translate();

11 }

12 

13 if (! array_key_exists('units', $value)) {

14 $fail('validation.quantity.missing_units')->translate();

15 }

16 }

17}

Predis 2.0

Dries Vints contributed support for Predis 2.0. Predis 2.0.0 is a maintenance release, so nothing should break.

Define nested “with” relations via a nested array

Tim MacDonald contributed a way to define nested eager loading relationships via a nested array:

1// Using dot notation

2$books = Book::with('author.contacts')->get();

3 

4// Nested array

5$books = Book::with([

6 'author' => [

7 'contacts',

8 'publisher',

9 ],

10])->get();

Add host methods to the Illuminate Request object

Pull Request #42797 provides three convenience methods on the Illuminate Request instance to access underlying Symphony methods:

1$request->host(); // getHost()

2$request->httpHost(); // getHttpHost()

3$request->schemeAndHttpHost(); // getSchemeAndHttpHost()

Invokable validation rules can push messages to nested attributes

Tim MacDonald contributed the ability for invokable validation rules to push errors to nested and other attributes:

1class UserRule implements InvokableRule

2{

3 public function __invoke($attribute, $value, $fail)

4 {

5 if (! is_array($attribute) || array_is_list($attribute)) {

6 return $fail('Must be an object.'); // apply to $attribute

7 }

8 

9 if (! array_key_exists('name', $attribute)) {

10 return $fail("{$attribute}.name", 'Is required.'); // apply to nested attribute

11 }

12 

13 /* ... */

14 }

15}

Given the above implementation, here’s a usage example:

1Validator::make(

2 [

3 'user_1' => ['xxxx'],

4 'user_2' => ['age' => 23],

5 ],

6 [

7 'user_1' => [new UserRule()],

8 'user_2' => [new UserRule()],

9 ]

10);

11 

12// errors...

13[

14 'user_1' => ['Must be an object.'],

15 'user_2.name' => ['Is required.'],

16]

Introduce fake() helper

Tim MacDonald contributed a global fake() helper function that allows you to easily access a singleton faker instance. Using this helper is helpful when prototyping, testing, and generating factory and seed data:

1@for($i = 0; $i < 10; $i++)

2 <dl>

3 <dt>Name</dt>

4 <dd>{{ fake()->name() }}</dd>

5 

6 <dt>Phone</dt>

7 <dd>{{ fake()->phoneNumber() }}</dd>

8 

9 </dl>

10@endfor

Here’s an example of locale-specific faker usage:

1fake()->name() // config('app.faker_locale') ?? 'en_US'

2fake('en_AU')->name() // en_AU

Cumulative query duration limit callback

Tim MacDonald contributed a callback handler after a cumulative query limit duration:

1DB::handleExceedingCumulativeQueryDuration(Interval::seconds(5), function (Connection $connection) {

2 Log::warning("Database queries exceeded 5 seconds on {$connection->getName()}");

3});

To learn more about this feature, check out the official documentation for cumulative query time. Also, check out Pull Request #42744 for implementation details and discussion around this feature.

Release Notes

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

v9.18.0

Added

  • Improve file attachment for mail and notifications (#42563)
  • Introduce Invokable validation classes (#42689)
  • Predis v2.0 (#42577)
  • Added Illuminate/View/Compilers/Concerns/CompilesConditionals::compileReadonly() (#42717)
  • Apply where’s from union query builder in cursor pagination (#42651)
  • Added ability to define “with” relations as a nested array (#42690)
  • Added ability to set backoff in broadcast events (#42737)
  • Added host(), httpHost(), schemeAndHttpHost() to Request (#42797)
  • Allow invokable rules to push messages to nested (or other) attributes (#42801)
  • Adds compilePushIf and compileEndpushIf functions to View compiler (#42762)
  • Added: Allow removing token during tests (#42841)
  • Added predefined_constants to reservedNames array in Illuminate/Console/GeneratorCommand (#42832)
  • Handle collection creation around a single enum (#42839)
  • Allow for nullable morphs in whereNotMorphedT (#42878)
  • Introduce a fake() helper to resolve faker singletons, per locale (#42844)
  • Allow handling cumulative query duration limit per DB connection (#42744)
  • Add invokable option to make rule command (#42742)

Fixed

  • Fix deprecation error in the route:list command (#42704)
  • Fixed Request offsetExists without routeResolver (#42754)
  • Fixed: Loose comparison causes the value not to be saved (#42793)
  • Fixed: Fix database session driver keeps resetting CSRF token (#42782)
  • Fixed: Arr::map – Fix map-by-reference w/ built-ins (#42815)
  • Fixed league/flysystem suggest (#42872)

Changed

  • Start session in TestResponse to allow marshalling of error bag from JSON (#42710)
  • Rename methods in Illuminate/Broadcasting/BroadcastManager (753e9fd)
  • Avoid throwing on invalid mime-type in Illuminate/Filesystem/FilesystemAdapter::mimeType() (#42761)
  • Do not resolve already set headers in Illuminate/Filesystem/FilesystemAdapter (#42760)
  • Standardise invokable rule translation functionality (#42873)
  • Clear cast cache when setting attributes using arrow (#42852)



[ad_2]

Source link

Leave a Reply

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