晚安社区,我有一个问题,我有一个从终端执行的命令,目标是将一封电子邮件发送到另一封电子邮件,因为一切都很好,对我来说效果很好,但是显示了我使用视图的电子邮件的内容,其内容非常简单,即“您已收到警报”,但我想从我的bd表中显示作业列表,通常是将变量链接到视图,使用控制器,但是在这种情况下它没有它,因为它只是作为内容来显示邮件中的内容,我试图以几种方式在命令中声明它,但我无法使其工作,我还尝试给它提供路径和控制器。在路由中,它正常地显示了具有变量的视图,但是在执行命令时,它告诉我它无法识别变量,如何使用所需的变量,在这种情况下为$ per。
这是命令代码:
<?php
namespace App\Console\Commands;
use App\pamatrizinfoperio;
use App\Periodicidad;
use App\Mail\SendMailable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class EnvAlert extends Command
{
//public $periodos;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'Send:Alert';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send Emails';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$pers = Periodicidad::select('paperiodicidad.des','pamatrizinfoperio.des', 'pamatrizinfoperio.codpar')
->join('pamatrizinfoperio', 'pamatrizinfoperio.cod_paperiodicidad', '=', 'paperiodicidad.cod')
->where('pamatrizinfoperio.read_at', '=', 0)
->get();
$data = array('name' => "Alert" , );
Mail::send('emails.welcome', $data, function($message) {
$message ->from('[email protected]', 'ALERT');
$message ->to('[email protected]')->subject('Alert');
});
return "The alert was send";
}
}
这是视图:
<!DOCTYPE html>
<html>
<head>
<title>Message Send</title>
</head>
<body>
<ul class="list-group">
@foreach($pers as $per)
<li class="list-group-item">
{{$per->des}}
{{$per->des}}
</li>
@endforeach
@endforeach
</ul>
</body>
</html>
我在其他视图中具有相同的变量,并且在代码中具有相同的查询,并且仅当我在控制器中声明它们时,它才能正常工作,但是在这种情况下,我不知道为什么它对我不起作用,希望您可以为我提供所需的任何数据。抱歉,如果我的英语不太好,在此方面,我将非常感谢您的帮忙,因为您会注意到系统的功能可以这么说,它只是发送电子邮件通知工作和完成工作的时间。预先感谢您的宝贵时间。
您正在将名为$data
的变量传递到邮件,并且正在邮件视图中访问其他变量
在您的情况下,您的句柄函数应该像这样
public function handle(){
$pers = Periodicidad::select('paperiodicidad.des','pamatrizinfoperio.des', 'pamatrizinfoperio.codpar')
->join('pamatrizinfoperio', 'pamatrizinfoperio.cod_paperiodicidad', '=', 'paperiodicidad.cod')
->where('pamatrizinfoperio.read_at', '=', 0)
->get();
$data = ['name' => 'Alert',
'pers' => $pers];
Mail::send('emails.welcome', $data, function($message) {
$message ->from('[email protected]', 'ALERT');
$message ->to('[email protected]')->subject('Alert');
});
return "The alert was send";
}
然后您将可以在邮件视图中访问$pers
,希望对您有所帮助:)