vendredi 30 octobre 2015

Laravel event listeners undefined property error with ShouldQueue

I have an event setup within my application that sends out an activation email upon the user signing up. It works perfectly without the ShouldQueue interface. However, when I add implements ShouldQueue, I get undefined property error:

Undefined property: App\Events\User\UserCreated::$user

The code I am using is as follows:

<?php

namespace App\Listeners\User;

use App\Events\User\UserCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Repos\Email\EmailTemplateRepoInterface;
use App\Repos\User\UserRepoInterface;
use Request, Mail, DbView;

class UserCreatedEmail implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(EmailTemplateRepoInterface $template)
    {
        $this->template = $template;
    }

    /**
     * Handle the event.
     *
     * @param  UserCreated  $event
     * @return void
     */
    public function handle(UserCreated $event)
    {
        $user = $event->user;

        if($user)
        {
            if(!$user->status)
            {
                $user->activation_url = Request::root().'/activate/'.$user->activation_code;
                $template = $this->template->findTemplateByName('new_user_activation');
                $userArr = $user->toArray();
            }

            Mail::queue([], [], function ($message) use ($template, $userArr) 
            {
                $message->to($userArr['email'])
                    ->subject($template->subject)
                    ->setBody(DbView::make($template)->with($userArr)->render(), 'text/html');
            });
        }
    }
}

Can anyone explain why this happens when I try to queue the email?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire