2017-08-02 37 views
1

我试图发送电子邮件给管理员使用mailable api接受的用户在Laravel 5.3。传递给App Mail SurveyMail :: __构造()的参数1必须是App Mail User的一个实例,array给

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]'); 
    } 

,这是我的控制器

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是不能接受的。请问我该如何解决这个问题?

回答

1

SurveyMail结构添加App\或在它的上面添加use App\User

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

然后调用应该是这样的:

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

感谢您的回复,但错误依然存在,甚至伴随着这种 – Sara

+0

@Sara检查我的更新:) – Maraboc

+0

是的,现在的工作非常非常感谢,但是为什么我有很多相同的邮件在我的mailtrap箱但是我发了一次。 – Sara

1

在你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> 
+0

感谢您的回复,但即使通过此修改,错误仍然存​​在 – Sara

+0

@Sara我更新了我的答案。尝试一下。 –

+0

谢谢它的作品 – Sara

0

你可以做以下哪些工作适合我。

从你SurveyMail构造,$user之前删除'User',使构造看起来像下面

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

这应该修正这个错误。

相关问题