【问题标题】:Argument 1 passed to App\Mail\SurveyMail::__construct() must be an instance of App\Mail\User, array give传递给 App\Mail\SurveyMail::__construct() 的参数 1 必须是 App\Mail\User 的实例,数组给出
【发布时间】:2018-01-09 13:04:49
【问题描述】:

我正在尝试使用 Laravel 5.3 中的 mailable api 向管理员接受的用户发送电子邮件。

class SurveyMail extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail.send')
        ->from('monsite@chezmoi.com');
    }

这是我的控制器

 class EmailController extends Controller
{
    public function send(Request $request,User $user)
    {    
         Mail::to($user)
         ->send(new SurveyMail ($request->except('_token')));
    }
}

观点:

<body style="background: black; color: white">
 <h2>Prise de contact sur mon beau site</h2>
    <p>Réception d'une prise de contact avec les éléments suivants :</p>
    <ul>

      <li><strong>Nom</strong> : {{ $user->name }}</li>
      <li><strong>Email</strong> : {{ $user->email }}</li>


</body>

似乎不接受传递给构造函数的参数User。请问,我该如何解决这个问题?

【问题讨论】:

    标签: laravel laravel-5.3


    【解决方案1】:

    SurveyMail 构造中添加App\ 或在其顶部添加use App\User

    public function __construct(App\User $user)
    {
        $this->user=$user;
    }
    

    然后调用应该是这样的:

    Mail::to($user)
          ->send(new SurveyMail ($user));
    

    【讨论】:

    • 感谢您的回复,但即使进行了修改,错误仍然存​​在
    • 是的,它现在可以完美运行了,非常感谢,但是为什么我的邮箱里有很多相同的邮件,但是我发送了一次。
    • 尝试删除所有这些并再试一次
    • 删除后还是一样
    • 好的,这个send函数是怎么调用的?
    【解决方案2】:

    在您的 SurveyMail 构造函数中,您已将提示 $user 键入为 User 对象,但在实例化该类时,您已传递了作为数组的请求数据。试试这个

    Mail::to($user)->send(new SurveyMail($user));
    

    此外,User 对象没有导入,因此它假定您的 User 类在 App\Mail 中。事实并非如此。因此,将您的 App\User 模型导入您的班级。

    use App\User; // <- Add this here
    
    class SurveyMail extends Mailable
    

    另外,在您看来。你忘了关闭ul 标签。

    <body style="background: black; color: white">
     <h2>Prise de contact sur mon beau site</h2>
        <p>Réception d'une prise de contact avec les éléments suivants :</p>
        <ul>
          <li><strong>Nom</strong> : {{ $user->name }}</li>
          <li><strong>Email</strong> : {{ $user->email }}</li>
        </ul>
    </body>
    

    【讨论】:

    • 感谢您的回复,但即使进行此修改,错误仍然存​​在
    【解决方案3】:

    您可以执行以下对我有用的操作。

    从您的 SurveyMail 构造函数中,删除 'User' 之前的 $user 并使构造函数如下所示

    public function __construct($user)
    {
        $this->user = $user;
    }
    

    这应该可以修复错误。

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      相关资源
      最近更新 更多