Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

overriding postEmail() results in strange error

Because I don't want users in laravel to enter their email to reset their password, I want to use the username as identifier for password reset. I changed the trait to:

public function postEmail(Request $request)
    {
        $this->validate($request, ['username' => 'required']);
        $response = Password::sendResetLink($request->only('username'), function (Message $message) {
            $message->subject($this->getEmailSubject());
        });
        switch ($response) {
            case Password::RESET_LINK_SENT:
            //nicht mir error sondern mit status antworten
                return redirect()->back()->withErrors(['username' => 'asasa']);
            case Password::INVALID_USER:
                return redirect()->back()->withErrors(['username' => trans($response)]);
        }
    }

When I tested this it works fine. Now I included it into my PasswordController and I'm getting this as error message shown:

ErrorException in PasswordController.php line 43: Argument 1 passed to App\Http\Controllers\Auth\PasswordController::App\Http\Controllers\Auth{closure}() must be an instance of App\Http\Controllers\Auth\Message, instance of Illuminate\Mail\Message given

The line43 contains:

    $response = Password::sendResetLink($request->only('username'), function (Message $message) {
        $message->subject($this->getEmailSubject());
    });

My includes in my PasswordController are:

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Password;

All in all this error seems very strange to me. Editing the trait works fine, but I want to exclude it, because editing code directly in traits isn't fine style.

0 likes
1 reply
BrandonSurowiec's avatar
Level 14

You forgot to import the namespace for Message at the top of your PasswordController.php file.

use Illuminate\Mail\Message;

Or you could simply not do type checking and do this instead (which I would recommend..):

// notice how we don't care what type $message is. 
// We'll trust that the right type will be passed in
 $response = Password::sendResetLink($request->only('username'), function ($message) { 
2 likes

Please or to participate in this conversation.