'Too few arguments to function App\Mail\OrderShipped::__construct() - Laravel 9

I searched for sending email, and I have no idea why that code isn't working. So I tried to create a theme for auto-send emails for users with password reset or other stuff.

I found out that I can create in mail.php all secrets for the email account, and that would work. And all things are working correctly to the moment that I was trying to add some variables to the email thru Mailable.

MailController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\OrderShipped;
use App\Models\User;

class MailController extends Controller
{
    public function test(Request $request) 
    {
        $user = User::find(Auth::user()->id);
        Mail::to($user->email)->send(new OrderShipped($user));

        return 'tak';
    }
}

web.php

Route::get('/test', [MailController::class, 'test']);

OrderShipped.php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

use App\Models\User;

class OrderShipped extends Mailable
{
    use Queueable, SerializesModels;

    public $username;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($username)
    {
        // $this->username = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('[email protected]', 'Test')
                    ->view('emails.basic');
    }
}

After all I've got an error like:

Too few arguments to function App\Mail\OrderShipped::__construct(), 0 passed in laravel->serializable-closure://function () { \Illuminate\Support\Facades\Mail::to(\Auth::user())->send(new \App\Mail\OrderShipped); return \view('welcome'); } on line 3 and exactly 1 expected

and laravel made this line red in Exeption Handler:

public function __construct($username)

Any ideas what's wrong? In Laravel 9 docks was a very similiar example.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source