我如何 运行 控制器上的 foreach?

How do I run a foreach on a controller?

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

我的控制器:

<?php

namespace App\Console\Commands;

use App\p;
use App\User;
use App\Pe;
use App\Mail\SendMailable;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;

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 App\Console\Commands;

use App\p;
use App\User;
use App\Pe;
use App\Mail\SendMailable;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;

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";
    }
    }
}

您在这一部分中缺少 $ 字符:

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

必须是:

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

您的查询是:

$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();    

它returns一个集合。你需要 foreach $sis 并制作一个电子邮件数组或使用像 first(), last()...

这样的方法

像这样:

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