vendredi 4 août 2017

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('support@dev.com')
                        ->view('emails.mondayEmail');
        }
    }

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

Here's my mail view:

@foreach ($events as $event)
    
@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('dev@gmail.com')->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.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2v4Iu2R
via IFTTT

Aucun commentaire:

Enregistrer un commentaire