Search code examples
laravellaravel-5laravel-mail

Laravel email, pass variables


I am trying to build emails in Laravel, and I have a trouble with this one, I want to send email every Monday therefore I want to trigger it as a command and schedule it (unless there is a better way?) Here's my email:

<?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('[email protected]')
                        ->view('emails.mondayEmail');
        }
    }

At this point $events does bring me back a collection.

Here's my mail view:

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

and command:

<?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('[email protected]')->send(new MondayEmails());

    }
}

and when I run it I get:

[ErrorException] Undefined variable: events (View: C:\xampp\htdocs\liveandnow\resources\views\emails\mondayEmail.blade.php)

[ErrorException] Undefined variable: events

How can this be solved? Is this the right approach? Or you would do it differently.


Solution

  • The constructor doesn't return, it allows you to set properties to access within the classes other functions. If you try the following code in your MondayEmails class, this will allow you to access the emails returned in the constructor.

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