You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Mailables, all public properties are made available to the view, that's expected behavior. But if the mailable contains a public (typed) variable that hasn't been initialized, the buildViewData() method throws an exception Typed property App\Mail\YourClass::$variable must not be accessed before initialization.
I think there should be a check on buildViewData() to make sure typed variables are initialized before being passed to the view.
Steps To Reproduce
php artisan make:mail OrderShipped
<?phpnamespaceApp\Mail;
useApp\Models\User;
useIlluminate\Bus\Queueable;
useIlluminate\Contracts\Queue\ShouldQueue;
useIlluminate\Mail\Mailable;
useIlluminate\Mail\Mailables\Content;
useIlluminate\Mail\Mailables\Envelope;
useIlluminate\Queue\SerializesModels;
class OrderShipped extends Mailable
{
publicUser$user;
use Queueable, SerializesModels;
/** * Create a new message instance. */publicfunction__construct()
{
//
}
Then, try to send this mailable, from tinker:
$mail = $mail = new App\Mail\OrderShipped();
Mail::to("mail@example.org")->send($mail);
It will throw an exception
Error Typed property App\Mail\OrderShipped::$user must not be accessed before initialization.
The text was updated successfully, but these errors were encountered:
driesvints
changed the title
Non-initialized public typed properties on mailables throw an exception.
Non-initialized public typed properties on mailables throw an exception
Jan 25, 2024
Laravel Version
10.42.0
PHP Version
8.2.7
Database Driver & Version
No response
Description
When using Mailables, all public properties are made available to the view, that's expected behavior. But if the mailable contains a public (typed) variable that hasn't been initialized, the
buildViewData()
method throws an exceptionTyped property App\Mail\YourClass::$variable must not be accessed before initialization.
I think there should be a check on
buildViewData()
to make sure typed variables are initialized before being passed to the view.Steps To Reproduce
php artisan make:mail OrderShipped
Then, try to send this mailable, from tinker:
It will throw an exception
Error Typed property App\Mail\OrderShipped::$user must not be accessed before initialization.
The text was updated successfully, but these errors were encountered: