laravel 传递给 App\Mail\SurveyMail::__construct() 的参数 1 必须是 App\Mail\User 的一个实例,数组给出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45458565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:25:51  来源:igfitidea点击:

Argument 1 passed to App\Mail\SurveyMail::__construct() must be an instance of App\Mail\User, array give

laravellaravel-5.3

提问by Sara

I'm trying to send emails to the users who have been accepted by the admin using mailable api in Laravel 5.3.

我正在尝试使用 Laravel 5.3 中的可邮寄 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('[email protected]');
    }

and this is my controller

这是我的控制器

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

the view:

风景:

<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>

it seems that the argument Userwhich is passed to the constructor is not accepted. Please, how can I fix this?

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

采纳答案by Maraboc

In the SurveyMailconstruct add App\or add use App\Useron the top of it :

SurveyMail构造中添加App\或添加use App\User到它的顶部:

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

And then the call should be like this :

然后调用应该是这样的:

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

回答by Zayn Ali

In your SurveyMailconstructor, you've type hinted $useras a Userobject but while instantiating the class you've passed the request data which is an array. Try this

在您的SurveyMail构造函数中,您已将类型提示$userUser对象,但在实例化类时,您已传递请求数据,该数据是一个数组。尝试这个

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

Also, there is no import for Userobject so it assumed that your Userclass is inside App\Mail. which it is not. So, import your App\Usermodel on top of your class.

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

use App\User; // <- Add this here

class SurveyMail extends Mailable

Also, in your view. you forgot to close the ultag.

另外,在你看来。你忘记关闭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>

回答by Joynal Abedin

You can do following which worked for me.

From your SurveyMailconstructor, remove 'User'before $userand make the constructor look like following

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

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

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

This should fix the error.

这应该可以修复错误。