Laravel 5.1 access event object in listener -
i trying out laravel 5.1's queue, having problem working $event object in listener.
authcontroller.php
public function postgenerateresettoken() { try { $admin = admin::where( 'email', '=', input::get( 'email' ) )->firstorfail(); $token = bus::dispatch( new generatepasswordresettoken( $admin ) ); event( new passwordresettokenwasgenerated( $admin, $token ) ); return success(); } catch( modelnotfoundexception $exception ) { return fail(); } }
passwordresettokenwasgenerated.php
class passwordresettokenwasgenerated extends event { use serializesmodels; public function __construct( $admin, $token ) { $this->admin = $admin; $this->token = $token; } public function broadcaston() { return []; } }
sendforgottenpasswordemail.php
class sendforgottenpasswordemail implements shouldqueue { public function __construct() { // } public function handle(passwordresettokenwasgenerated $event) { $data = [ 'admin' => $event->admin, 'token' => $event->token ]; mail::send( 'emails.forgotten-password', $data, function( $message ) use ( $event ) { $message->subject( 'forgotten password' ); $message->to( $event->admin->email ); }); } }
using $event->admin
in handler results in undefined property: passwordresettokenwasgenerated::$admin
but, error occurs when implement shouldqueue
interface on listener. works fine without interface.
the queue driver set sync
.
i know because of queue, isn't supposed work way want work?
you should declare admin , token public before setting them:
class passwordresettokenwasgenerated extends event {
use serializesmodels; public $admin; public $token; public function __construct( $admin, $token ) { $this->admin = $admin; $this->token = $token; } public function broadcaston() { return []; }
}
after should able access properties in listener.
Comments
Post a Comment