如何从视图中的命令访问变量?

时间:2020-05-14 03:26:06

标签: laravel

晚安社区。
我正在终端上执行命令,我想在邮件视图中访问$per变量,这给我一个错误,提示它无法识别{{ 1}}变量。

这是命令代码:

$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('coop@gmail.com', 'ALERT');
        $message ->to('coop@gmail.com')->subject('Alert');
    });
    return "The alert was send";
    }
}

我在其他视图中具有相同的变量,并且在代码中具有相同的查询,并且仅当我在控制器中声明它们时,它才能正常工作,但是在这种情况下,我不知道为什么它对我不起作用,我希望您可以为我提供所需的任何数据。抱歉,如果我的英语不太好,在此方面,我将非常感谢您的帮忙,因为您会注意到系统的功能可以这么说,它只是发送电子邮件通知工作和完成工作的时间。预先感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

您正在将名为$data的变量传递给邮件,并且正在邮件视图中访问其他变量

在这种情况下,您的handle函数应该像这样

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('coop@gmail.com', 'ALERT');
        $message ->to('coop@gmail.com')->subject('Alert');
    });
    return "The alert was send";
}

然后,您将可以在邮件视图中访问$pers, 我希望它对您有帮助:)

相关问题