如何在控制器上运行 foreach?



我想发送几封电子邮件,以便将它们链接到转换表中,一切都很好,问题是当我使用该命令时,它只向我发送一封电子邮件,并且每个用户都不会重复它,因为当使用 foreach 时,我应该为每个用户发送电子邮件, 同时分别查看每个查询,如果有多个用户抓住我,所以我不知道为什么该命令只向我发送一封电子邮件。我希望你能帮助我,另一方面我也想使用 sis 变量的电子邮件,但是在"邮件"中使用它时,它会告诉我该变量不存在。事先感谢大家。

我的控制器:

<?php
namespace AppConsoleCommands;
use Appp;
use AppUser;
use AppPe;
use AppMailSendMailable;
use IlluminateConsoleCommand;
use CarbonCarbon;
use IlluminateSupportFacadesMail;
class Alertc extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'S:AC';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send works';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{

$hour= 14;
$minute= 0;
$second=0;
$year= date("Y");
$month= date("m");
$day=date("d");
$hourM=date("H");
$dayy= now();
$datelim=Carbon::Create($year,$month,$day,$hour,$minute,$second);
$datedif=Carbon::Create($year,$month,$day,$hourM,$minute,$second);
$dateen= $Datedif->addHour();
$intervalH= $dayy->diffInHours($datelim);
$intervalM= $dayy->diffInMinutes($dateen);
$systems = User::where('codarea','>',0)->get();
foreach ($systems as $system) {
$sis = User::select('users.email', 'users.id', 'users.name', 'p.description', 'p.Dateen')
->join('pa', 'pa.user', '=', 'users.id')
->join('p', 'p.coddate','=','pa.env')
->where('p.read_at', '=', 0, 'AND')
->where('pa.user', '=', $system->id)
->where('p.Dateen', '>', $dayy)
->get();     

$data = ['name' => "Alert" , 'periods' => $sis, 'Periodres' => $intervalH, 'Periodresm' => $intervalM,
'Hactual' => $dayy, 'Hlimit' => $dateend];
Mail::send('emails.welcome', $data, function($message) {
$message ->from('coopme@gmail.com', 'ALERT');
$message ->to('coopme@gmail.com')->subject('Alert');
});
return "La alert is finished";
}
}
}

我的观点:

<!DOCTYPE html>
<html>
<head>
<title>Message Sent</title>
</head>
<body>
<ul class="list-group">
Good morning the next works is:
@foreach($periods as $period)
<li class="list-group-item">
must send 
<b>{{$period->description}}</b>
@if($Hactual<$Hlimit)
the limit is
{{$Periodres}} Hours and
{{$Periodresm}} Minutes.
@elseif($Hactual>$Hlimit)
is finished. 
@endif

</li>
@endforeach
<b><h6>Always with you</h6></b>
</ul>
</body>
</html>

当我想在邮件中使用 SIS 变量时,这是我的控制器:

namespace AppConsoleCommands;
use Appp;
use AppUser;
use AppPe;
use AppMailSendMailable;
use IlluminateConsoleCommand;
use CarbonCarbon;
use IlluminateSupportFacadesMail;
class Alertc extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'S:AC';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send works';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{

$hour= 14;
$minute= 0;
$second=0;
$year= date("Y");
$month= date("m");
$day=date("d");
$hourM=date("H");
$dayy= now();
$datelim=Carbon::Create($year,$month,$day,$hour,$minute,$second);
$datedif=Carbon::Create($year,$month,$day,$hourM,$minute,$second);
$dateen= $Datedif->addHour();
$intervalH= $dayy->diffInHours($datelim);
$intervalM= $dayy->diffInMinutes($dateen);
$systems = User::where('codarea','>',0)->get();
foreach ($systems as $system) {
$sis = User::select('users.email', 'users.id', 'users.name', 'p.description', 'p.Dateen')
->join('pa', 'pa.user', '=', 'users.id')
->join('p', 'p.coddate','=','pa.env')
->where('p.read_at', '=', 0, 'AND')
->where('pa.user', '=', $system->id)
->where('p.Dateen', '>', $dayy)
->get();     

$data = ['name' => "Alert" , 'periods' => $sis, 'Periodres' => $intervalH, 'Periodresm' => $intervalM,
'Hactual' => $dayy, 'Hlimit' => $dateend];
Mail::send('emails.welcome', $data, function($message) {
$message ->from('coopme@gmail.com', 'ALERT');
$message ->to(sis->email)->subject('Alert');
});
return "La alert is finished";
}
}
}

您的查询是:

$sis = User::select('users.email', 'users.id', 'users.name', 'p.description', 'p.Dateen')
->join('pa', 'pa.user', '=', 'users.id')
->join('p', 'p.coddate','=','pa.env')
->where('p.read_at', '=', 0, 'AND')
->where('pa.user', '=', $system->id)
->where('p.Dateen', '>', $dayy
->get();    

它返回一个集合。您需要 foreach $sis 并创建一个电子邮件数组或使用类似 first((、last((...

像这样:

$message ->to($sis->first()->email)->subject('Alert'); //To send to the first user of your query

本节中缺少 $ 字符:

$message ->to(sis->email)->subject('Alert');

必须是:

$message ->to($sis->email)->subject('Alert');

最新更新