【问题标题】:How do I run a foreach on a controller?如何在控制器上运行 foreach?
【发布时间】:2020-06-15 02:45:54
【问题描述】:

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

我的控制器:

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

【问题讨论】:

    标签: laravel foreach command


    【解决方案1】:

    您的查询是:

    $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
    

    【讨论】:

    • 错误:“未定义的变量:sis”它可能是什么:(
    • 你必须明确告知你想在 PHP 的匿名函数中使用 $sis,所以,这样做: Mail::send('emails.welcome', $data, function($message) use ($sis) {
    【解决方案2】:

    您在本节中缺少 $ 字符:

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

    必须是:

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

    【讨论】:

    • $message ->to($sis->email)->subject('Alert');我更改了它,在我看来“未定义变量:sis”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多