【问题标题】:Laravel email, pass variablesLaravel 电子邮件,传递变量
【发布时间】:2017-08-04 09:30:30
【问题描述】:

我正在尝试在 Laravel 中构建电子邮件,但我遇到了问题,我想每周一发送电子邮件,因此我想将其作为命令触发并安排它(除非有更好的方法?)这里是我的电子邮件:

<?php

namespace App\Mail;

use App\Event;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

    class MondayEmails extends Mailable
    {
        use Queueable, SerializesModels;

        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct()
        {
            $events = Event::limit(5)
                ->orderBy('title')
                ->get();
            return $events;
        }

        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            return $this->from('support@dev.com')
                        ->view('emails.mondayEmail');
        }
    }

此时 $events 确实给我带来了一个收藏。

这是我的邮件视图:

@foreach ($events as $event)
    {{ $event->title }}
@endforeach

和命令:

<?php

namespace App\Console\Commands;

use App\Event;
use App\Mail\MondayEmails;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class MondayEmail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'email:monday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Monday Events being send!';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(Request $request)
    {
        Mail::to('dev@gmail.com')->send(new MondayEmails());

    }
}

当我运行它时,我得到:

[ErrorException] 未定义变量:事件(查看: C:\xampp\htdocs\liveandnow\resources\views\emails\mondayEmail.blade.php)

[ErrorException] 未定义变量:事件

如何解决?这是正确的方法吗?或者你会做不同的事情。

【问题讨论】:

    标签: laravel laravel-5 laravel-mail


    【解决方案1】:

    构造函数不会返回,它允许您设置属性以在类中访问其他函数。如果您在 MondayEmails 类中尝试以下代码,这将允许您访问构造函数中返回的电子邮件。

    protected $events;
    public function __construct()
    {
        $this->events = Event::limit(5)
            ->orderBy('title')
            ->get();
    }
    
    public function build()
    {
        $events = $this->events;
        return $this->from('support@dev.com')
                    ->view('emails.mondayEmail', compact('events'));
    }
    

    【讨论】:

    • 仍然不起作用;/ [ErrorException] 为 foreach() 提供的参数无效
    • 它确实有效!我只是有语法错误thansk
    【解决方案2】:

    我认为你应该试试这个:

    protected $events;
    public function __construct()
    {
        $this->events = Event::limit(5)
            ->orderBy('title')
            ->get();
    }
    
    public function build()
    {
        $events = $this->events;
        return $this->from('support@dev.com')
                    ->view('emails.mondayEmail')
                    ->->with([
                    'events' => $events,
                  ]);
    }
    

    希望这对你有用!!!

    【讨论】:

    • 在这两个解决方案中我得到:[ErrorException] Undefined property: App\Mail\MondayEmails::$events
    • @PrzemekWojtas 让我检查一下
    • 我忘记了受保护的 $events;现在我得到 [ErrorException] 为 foreach() 提供的参数无效
    • @PrzemekWojtas 是的,因为您不能将 $events 变量与视图文件一起传递。
    • @PrzemekWojtas : 如果我的回答是你的解决方案,那么请接受我的回答
    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 2015-12-11
    • 2016-10-24
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多