Back to Blog

Laravel 9.35 Released

Julian Beaujardin
Julian Beaujardin Reference: Laravel News October 12th, 2022

The Laravel team released 9.35 with an exciting new alternate mailable syntax, an Eloquent "strict mode" feature, Load trashed models 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:

    namespace App\Mail;

    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Mail\Mailable;
    use Illuminate\Mail\Mailables\Address;
    use Illuminate\Mail\Mailables\Attachment;
    use Illuminate\Mail\Mailables\Content;
    use Illuminate\Mail\Mailables\Envelope;
    use Illuminate\Queue\SerializesModels;

    class InvoicePaid extends Mailable
    {
        use Queueable, SerializesModels;

        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }

        /**
         * Get the message envelope.
         *
         * @return \Illuminate\Mail\Mailables\Envelope
         */
        public function envelope()
        {
            return new Envelope(
                subject: 'Invoice Paid',
                cc: [new Address('foo@example.com', 'Example Name')],
                tags: [],
                metadata: [],
            );
        }

        /**
         * Get the message content definition.
         *
         * @return \Illuminate\Mail\Mailables\Content
         */
        public function content()
        {
            return new Content(
                view: 'html-view-name',
                text: 'text-view-name',
            );
        }

        /**
         * Get the attachments for the message.
         *
         * @return \Illuminate\Mail\Mailables\Attachment[]
         */
        public function attachments()
        {
            return [
                Attachment::fromPath('/path/to/file'),
            ];
        }
    }

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:

    Model::shouldBeStrict();

The shouldBeStrict() method is a shortcut for enabling all of the following:

    Model::preventLazyLoading();
    Model::preventSilentlyDiscardingAttributes();
    Model::preventsAccessingMissingAttributes();

Load trashed models with resource routes

Andrew Brown contributed the ability to load trashed models with resource routes using the following routing syntax:

    // All endpoints
    Route::resource('users', UserController::class)->withTrashed();

    // Only `show`
    Route::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.1 - 2022-10-11

Fixed

  • Remove check for $viewFactory->exists($component) in Illuminate/View/Compilers/ComponentTagCompiler::componentClass (7c6db00)

v9.35.0 - 2022-10-11

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)