Support the ongoing development of Laravel.io →
Mail Architecture Testing

Hello, I have recently updated Laravel from 5.0 to 5.2 but it seems the Event handling has changed a bit.

I keep getting this nasty error when I use $event variable in my Listener. Here is the incriminated code:


<?php 

namespace App\Listeners;

use App\Events\UserWasCreated;

use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

use Lang;

class UserCreated implements ShouldQueue
{
    /**
     * @var Mailer
     */
    private $mailer;
    
    /**
     * Create the event listener.
     *
     * @param Mailer $mailer
     */
    public function __construct( MailQueue $mailer )
    {
        $this->mailer = $mailer;
    }

    /**
     * Handle the event.
     *
     * @param  UserWasCreated  $event
     * @return void
     */

    public function handle( UserWasCreated $event )
    {
            $data = array('password' => $event->user->password, 'username' => $event->user->username, 'email' => $event->user->email);

            $this->mailer->queue('emails.user_created', $data, function($message) use ($data)
            {
                    $message->to($data['email'])->subject(Lang::get('notifications.USER_CREATED_EMAIL_SUBJECT', [], $event->user->lang));
            });
     }
}

Here is the error and stack trace:


[2016-03-21 00:57:57] local.ERROR: exception 'ErrorException' with message 'Undefined variable: event' in /Users/John/Sites/AppBackEnd/vendor/jeremeamia/SuperClosure/src/SerializableClosure.php(210) : eval()'d code:2

Stack trace:
#0 /Users/John/Sites/AppBackEnd/vendor/jeremeamia/SuperClosure/src/SerializableClosure.php(210) : eval()'d code(2): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/Users/John...', 2, Array)
#1 [internal function]: {closure}(Object(Illuminate\Mail\Message))
#2 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(401): call_user_func(Object(Closure), Object(Illuminate\Mail\Message))
#3 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(165): Illuminate\Mail\Mailer->callMessageBuilder(Object(Closure), Object(Illuminate\Mail\Message))
#4 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(278): Illuminate\Mail\Mailer->send('emails.user_cre...', Array, Object(Closure))
#5 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(129): Illuminate\Mail\Mailer->handleQueuedMessage(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#6 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Jobs/RedisJob.php(50): Illuminate\Queue\Jobs\Job->resolveAndFire(Array)
#7 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(210): Illuminate\Queue\Jobs\RedisJob->fire()
#8 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(159): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), '0', '0')
#9 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(110): Illuminate\Queue\Worker->pop('', 'default', '0', '3', '0')
#10 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(72): Illuminate\Queue\Console\WorkCommand->runWorker('', 'default', '0', '128', false)
#11 [internal function]: Illuminate\Queue\Console\WorkCommand->fire()
#12 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#13 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
#14 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(791): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /Users/John/Sites/AppBackEnd/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /Users/John/Sites/AppBackEnd/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /Users/John/Sites/AppBackEnd/artisan(36): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 {main}  
 
Last updated 2 years ago.
0

Its because of how anonymous functions work. You have to explicitly tell them what variables you want to use inside their scope.

PHP.net Anonymous functions Example #3.

Its not Laravel related but PHP related. You already have a use statement for that closure, so add your additional vars needed to it.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

anelito anelito Joined 8 Jun 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.