The Laravel team released 9.35 with an exciting new alternate mailable syntax, an Eloquent "strict mode" feature, and more.
Alternate mailable syntax
Taylor Otwell contributed an alternate mailable syntax to works by returning "slim value objects that specify the content and attributes of the mailable"
Here's an example from his pull request description:
1namespace App\Mail; 2 3use Illuminate\Bus\Queueable; 4use Illuminate\Contracts\Queue\ShouldQueue; 5use Illuminate\Mail\Mailable; 6use Illuminate\Mail\Mailables\Address; 7use Illuminate\Mail\Mailables\Attachment; 8use Illuminate\Mail\Mailables\Content; 9use Illuminate\Mail\Mailables\Envelope;10use Illuminate\Queue\SerializesModels;11 12class InvoicePaid extends Mailable13{14 use Queueable, SerializesModels;15 16 /**17 * Create a new message instance.18 *19 * @return void20 */21 public function __construct()22 {23 //24 }25 26 /**27 * Get the message envelope.28 *29 * @return \Illuminate\Mail\Mailables\Envelope30 */31 public function envelope()32 {33 return new Envelope(34 subject: 'Invoice Paid',35 cc: [new Address('foo@example.com', 'Example Name')],36 tags: [],37 metadata: [],38 );39 }40 41 /**42 * Get the message content definition.43 *44 * @return \Illuminate\Mail\Mailables\Content45 */46 public function content()47 {48 return new Content(49 view: 'html-view-name',50 text: 'text-view-name',51 );52 }53 54 /**55 * Get the attachments for the message.56 *57 * @return \Illuminate\Mail\Mailables\Attachment[]58 */59 public function attachments()60 {61 return [62 Attachment::fromPath('/path/to/file'),63 ];64 }65}
The traditional way of defining mailables using build()
will not be removed. I like how the above example is obvious what is happening using PHP 8's named arguments.
Eloquent "strict" mode
Chris Morrell and Taylor Otwell collaborated on an Eloquent strict mode, which enables the following:
- No lazy loading
- Exceptions when assigning non-fillable attributes
- Exceptions accessing attributes that weren't retrieved or didn't exist
Ideally, you'll use strict mode in development by adding the following to the boot()
method of a registered service provider:
1Model::shouldBeStrict();
The shouldBeStrict() method is a shortcut for enabling all of the following:
1Model::preventLazyLoading();2Model::preventSilentlyDiscardingAttributes();3Model::preventsAccessingMissingAttributes();
Load trashed models with resource routes
Andrew Brown contributed the ability to load trashed models with resource routes using the following routing syntax:
1// All endpoints2Route::resource('users', UserController::class)->withTrashed();3 4// Only `show`5Route::resource('users', UserController::class)->withTrashed(['show']);
Release Notes
You can see the complete list of new features and updates below and the diff between 9.34.0 and 9.35.0 on GitHub. The following release notes are directly from the changelog:
v9.35.0
Added
- Allow loading trashed models for resource routes (#44405)
- Added
Illuminate/Database/Eloquent/Model::shouldBeStrict()
and other (#44283) - Controller middleware without resolving controller (#44516)
- Alternative Mailable Syntax (#44462)
Fixed
- Fix issue with aggregates (withSum, etc.) for pivot columns on self-referencing many-to-many relations (#44286)
- Fixes issue using static class properties as blade attributes (#44473)
- Traversable should have priority over JsonSerializable in EnumerateValues (#44456)
- Fixed
make:cast --inbound
so it's a boolean option, not value (#44505)
Changed
- Testing methods. Making error messages with json_encode more readable (#44397)
- Have 'Model::withoutTimestamps()' return the callback's return value (#44457)
- only load trashed models on relevant routes (#44478)
- Adding additional PHP extensions to shouldBlockPhpUpload Function (#44512)
- Register cutInternals casters for particularly noisy objects (#44514)
- Use get methods to access application locale (#44521)
- return only on non empty response from channels (09d53ee, 3944a3e)
- Correct channel matching (#44531)
- Migrate mail components (#44527)
0 comments:
Post a Comment
Thanks