Php 控制器操作中使用Laravel 5.6发送电子邮件时出错
我收到错误Php 控制器操作中使用Laravel 5.6发送电子邮件时出错,php,laravel,Php,Laravel,我收到错误未定义属性:stdClass::$email 我的控制器操作中的代码: ... use App\Mail\WelcomeMail; use Illuminate\Support\Facades\Mail; public function approve($id){ $mail = DB::select('select pic_email from vendors where id = :id', ['id' => $id]); $user = DB::sele
未定义属性:stdClass::$email
我的控制器操作中的代码:
...
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
public function approve($id){
$mail = DB::select('select pic_email from vendors where id = :id', ['id' => $id]);
$user = DB::select('select roc_no from vendors where id = :id', ['id' => $id]);
Mail::to($mail)->send(new WelcomeMail($user));
return redirect('/account/pending')->with('success', 'Account approved!');
}
在app\Mail\WelcomeMail.php中:
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.welcome');
}
}
我按照来自的教程编写代码
我使用邮件陷阱。由于Laravel内置的密码重置功能正在工作,因此.env中的所有凭据/配置都是正确的。那么我的代码怎么了
更新:我将跟踪错误,可能来自控制器操作,即$mail=DB::select('select pic_email from vendors,其中id=:id',['id'=>$id])代码>
我正在使用SQL返回值。然后我尝试使用tinker将值$mail
作为数组返回。我认为是这个问题导致无法获取$mail
如何纠正这一点?使用extract()
也不会给出任何结果。DB::select
函数返回一个对象数组DB::selectOne
将返回一个按列名索引的对象
您还可以将两个查询合并为一个查询,然后按如下方式访问列名:
public function approve($id)
{
$vendor = DB::selectOne('select pic_email, roc_no from vendors where id = :id', compact('id'));
Mail::to($vendor->pic_email)->send(new WelcomeMail($vendor->roc_no));
return redirect('/account/pending')->with('success', 'Account approved!');
}
不需要两个不同的查询您可以在一个查询中完成此操作
$user = DB::selectOne('select roc_no,pic_email from vendors where id = :id', ['id' => $id]);
OR
$user = DB::table('vendors')->where('id',$id)->select('roc_no','pic_email')->first();
// $user->pic_email; email
// $user->roc_no; ROC NO
如果需要,您可以进行测试!通过tinker获得如下代码
做测试,它会告诉你是否有错误
# SSH into droplet
# go to project
$ php artisan tinker
$ Mail::send('errors.401', [], function ($message) { $message->to('emmanuelbarturen@gmail.com')->subject('this works!'); });
# check your mailbox
我遇到另一个错误,无法将stdClass类型的对象用作数组
。是因为to()方法不接受数组吗?@AzwanAbdullah我错了,返回的值是stdClass
的一个实例。请参阅我的最新答案。