You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Mailable类传参失败:无法传递User对象与密码

Hey there! Let's troubleshoot this together—passing data to Laravel Mailables can be tricky if you miss a small step, but I’ve got you covered based on common pitfalls and the info you shared.

Step 1: Fix Your Mailable Class

First, make sure your Mailable is properly accepting and exposing the User object and password. You have two solid options here:

Option 1: Use Public Properties (Simplest)

Declare $user and $password as public properties in your Mailable—Laravel automatically makes these available to your Blade view:

use App\Models\User;
use Illuminate\Mail\Mailable;

class YourCustomMailable extends Mailable
{
    // Public properties are auto-passed to the view
    public $user;
    public $password;

    // Match the parameter order with how you'll call it in the controller
    public function __construct(User $user, string $password)
    {
        $this->user = $user;
        $this->password = $password;
    }

    public function build()
    {
        return $this->view('emails.your-view-template');
    }
}

Option 2: Use with() for Explicit Data Passing

If you prefer keeping properties private (for encapsulation), use the with() method in your build() function to pass data to the view:

use App\Models\User;
use Illuminate\Mail\Mailable;

class YourCustomMailable extends Mailable
{
    private $user;
    private $password;

    public function __construct(User $user, string $password)
    {
        $this->user = $user;
        $this->password = $password;
    }

    public function build()
    {
        return $this->view('emails.your-view-template')
            ->with([
                'user' => $this->user,
                'password' => $this->password,
            ]);
    }
}

Step 2: Verify Your Controller Call

Double-check that you’re passing both the User object and password correctly when dispatching the Mailable. The parameter order must match what your Mailable’s constructor expects:

use App\Mail\YourCustomMailable;
use App\Models\User;
use Illuminate\Support\Facades\Mail;

public function sendUserCredentials()
{
    // Fetch your User (adjust this to your use case)
    $user = User::where('email', 'user@example.com')->first();
    // Your password string (you mentioned this is in the payload, so confirm it's a valid string here)
    $password = 'your-generated-password';

    // Send the email—make sure both parameters are included!
    Mail::to($user->email)->send(new YourCustomMailable($user, $password));

    // Or if using queues:
    // Mail::to($user->email)->queue(new YourCustomMailable($user, $password));

    return redirect()->back()->with('status', 'Credentials sent!');
}

Step 3: Check Your Blade View

Ensure you’re referencing the variables correctly in your view. No matter which option you used above, the variable names will match what you passed:

<!-- resources/views/emails/your-view-template.blade.php -->
<h2>Hi {{ $user->name }},</h2>
<p>Here are your account details:</p>
<ul>
    <li>Email: {{ $user->email }}</li>
    <li>Password: {{ $password }}</li>
</ul>

Common Pitfalls to Rule Out

  • Parameter Order Mismatch: If you pass $password first then $user in the controller, but your Mailable expects User first, you’ll get errors. Always match the constructor’s order.
  • Forgot to Clear Cache: Laravel sometimes caches Mailable constructors. Run these commands to clear cached files:
    php artisan clear-compiled
    php artisan cache:clear
    
  • Private Properties Without with(): If you used private properties but didn’t pass them via with(), the view won’t have access to them—stick with public properties or explicitly pass data.

Since you mentioned the password is in the payload, it’s likely a small issue with how the Mailable exposes the data to the view. Walk through these steps, and you should have it working in no time!

内容的提问来源于stack exchange,提问作者Edmund Sulzanok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:19:38