在索引视图中,我显示了所有用户,并且有一个按钮会将用户状态更改为活动和不活动。代码如下:
@foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->surname}}</td>
<td>{{$user->email}}</td>
<td>
@if($user->is_active == 0)
{!! Form::model($user, ['method' => 'PUT', 'action'=>['AdminUserController@activateuser', $user->id]]) !!}
{!! Form::submit('Activate', ['class'=>'btn btn-primary']) !!}
{!! Form::close() !!}
@else
{!! Form::model($user, ['method' => 'PUT', 'action'=>['AdminUserController@activateuser', $user->id]]) !!}
{!! Form::submit('De-Activate', ['class'=>'btn btn-danger']) !!}
{!! Form::close() !!}
@endif
</td>
<td>{{$user->cell}}</td>
<td><button class="btn btn-primary">View Property</button></td>
<td><button class="btn btn-danger">Delete</button></td>
</tr>
@endforeach
因此,当我单击激活/停用按钮时,会触发控制器的activateuser功能。激活后,将发送一封电子邮件。 控制器如下所示:
public function activateuser(Request $request, $id){
$user = User::findOrFail($id);
if($user->is_active == 0){
$user->update([$user->is_active = 1]);
Mail::send(new activateUser());
}
else{
$user->update([$user->is_active = 0]);
}
return redirect()->back();
}
此刻,电子邮件将发送给自己,我的Mailabçe如下所示:
public function build()
{
return $this->view('emails.activateuser')->to('wosleybago@gmail.com');
}
我想要的是将电子邮件从数据库表中的用户电子邮件发送到电子邮件地址。
我该怎么做?
所以,我应该得到$ user-> email
答案 0 :(得分:1)
在文档https://laravel.com/docs/5.6/mail#sending-mail
中描述了发送电子邮件将此代码放在activateUser()函数中
Mail::to($user)->send(new YourMailableName());
不要忘记使用“ use”关键字导入Mail和YourMailableName。
或者您可以使用用户电子邮件代替对象
Mail::to($user->email)->send(new YourMailableName());
然后从您的Mailable /
中删除->至('wosleybago@gmail.com')答案 1 :(得分:1)
在创建新的ActivateUser实例时,您应该传递用户电子邮件,
Mail::send(new activateUser($user->email));
然后再使用此属性。
答案 2 :(得分:1)
我通常希望我的电子邮件在自身中包含所有信息,因此我通过User
实例或任何包含撰写邮件所需数据的实例。
因此Mailable
具有__construct(..)
,如下所示:
/**
* @var \App\User
*/
public $user; // since this is a public property its going to be available in view of mailable as $user
__construct(App\User $user) {
$this->user = $user;
// further more I set the to(), this is what you are after
$this->to($user->email, $user->name);
// and subject
$this->subject('You are activated!');
}
...
现在您需要在控制器中执行的所有操作如下:
Mail::send(new activateUser($user));
如上所述,$user
在邮件视图中可用,因此您也可以在其中使用它:
Hi, {{ $user->name }},
...
注意:将
activateUser
从ActivateUser
更改为follow PSR-2类名称必须在 StudlyCaps 中声明。
我还使用排队的邮件,因此我在$timeout
类上设置了$tries
和Mailable
属性。
答案 3 :(得分:1)
Sending Email to particular a person is quite simple. My suggestion would be as follows:
In existing code you can get the email of the current user and send it using a helper to() that comes with mail functionality of laravel. You can code it like this.
if($user->is_active == 0){
$user->update([$user->is_active = 1]);
Mail::to($user->email)->send(new MailableClassInstance);
}