Laravel电子邮件,传递变量

Laravel电子邮件,传递变量,laravel,laravel-5,laravel-mail,Laravel,Laravel 5,Laravel Mail,我正在尝试在Laravel中创建电子邮件,但我遇到了一个问题,我想每周一发送一封电子邮件,因此我想将其作为命令触发并安排它(除非有更好的方法?)以下是我的电子邮件: <?php namespace App\Mail; use App\Event; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Qu

我正在尝试在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');
        }
    }
指挥部:

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

    }
}

构造函数不返回,它允许您设置要在类和其他函数中访问的属性。如果您在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'));
}

我想你应该试试这个:

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]未定义的属性:App\Mail\MondayEmails::$events@PrzemekWojtas让我检查一下itI忘记了受保护的$events;现在我得到的[ErrorException]为foreach()@PrzemekWojtas Yes提供的参数无效,因为您无法将$events变量与视图文件一起传递。@PrzemekWojtas:如果我的答案是您的解决方案,请接受我的答案仍然不起作用;/[ErrorException]为foreach()提供的参数无效。它确实有效!我只是有语法错误
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,
              ]);
}