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
Argument 1 passed to App\Mail\SurveyMail::__construct() must be an instance of App\Mail\User, array give
提问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 User
which is passed to the constructor is not accepted. Please, how can I fix this?
似乎User
不接受传递给构造函数的参数。请问,我该如何解决这个问题?
采纳答案by Maraboc
In the SurveyMail
construct add App\
or add use App\User
on 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 SurveyMail
constructor, you've type hinted $user
as a User
object but while instantiating the class you've passed the request data which is an array. Try this
在您的SurveyMail
构造函数中,您已将类型提示$user
为User
对象,但在实例化类时,您已传递请求数据,该数据是一个数组。尝试这个
Mail::to($user)->send(new SurveyMail($user));
Also, there is no import for User
object so it assumed that your User
class is inside App\Mail
. which it is not. So, import your App\User
model 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 ul
tag.
另外,在你看来。你忘记关闭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 SurveyMail
constructor, remove 'User'
before $user
and make the constructor look like following
您可以执行以下对我有用的操作。
从您的SurveyMail
构造函数中,删除'User'
之前$user
并使构造函数如下所示
public function __construct($user)
{
$this->user = $user;
}
This should fix the error.
这应该可以修复错误。